This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
How to convert string "Monday, 10 January 2017 | 04:58 PM" to a java.sql.Date 2017-01-10?
Use SimpleDateFormat:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
class Main {
public static void main(String[] args) {
try {
SimpleDateFormat format = new SimpleDateFormat("EEEEEE, dd MMMMMMM yyyy | hh:mm a");
Date parsed = format.parse("Monday, 10 January 2017 | 04:58 PM");
java.sql.Date sql = new java.sql.Date(parsed.getTime());
} catch (ParseException e){
e.printStackTrace();
}
}
}
Related
This question already has answers here:
Converting String to Date using SimpleDateFormat is returning random date [duplicate]
(2 answers)
how to parse output of new Date().toString()
(4 answers)
Change date format in a Java string
(22 answers)
Convert String Date to String date different format [duplicate]
(8 answers)
How to parse date string to Date? [duplicate]
(6 answers)
Closed 3 years ago.
I have a date time string like this:
Java Code:
String datetimeString = "Tue Apr 10 15:19:06 CEST 2018";
I would like to convert this to a date like this: 2018-04-10
How can I do this?
I have tried this:
Date result;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
result = formatter.parse (datetimeString);
But I get "Unparseable date"
Well since you already applied the brute force method, here's how to use the API.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateTest {
public static void main(String[] args) throws Exception {
String datetimeString = "Tue Apr 10 15:19:06 CEST 2018";
String from_format = "E MMM dd HH:mm:ss z yyyy";
String to_format = "yyyy-MM-dd";
DateTimeFormatter from_formatter = DateTimeFormatter.ofPattern(from_format);
DateTimeFormatter to_formatter = DateTimeFormatter.ofPattern(to_format);
LocalDateTime ldt = LocalDateTime.parse(datetimeString, from_formatter);
System.out.println(ldt.format(to_formatter));
}
}
In the DateTimeFormatter class it is important to understand the format symbol AND the
presentation descriptions for proper symbol count.
This question already has answers here:
Why does sdf.format(date) converts 2018-12-30 to 2019-12-30 in java? [duplicate]
(3 answers)
Java SimpleDateFormat returning wrong value in Date object
(1 answer)
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 3 years ago.
Simple test case below is giving results different than expected.
import javax.xml.datatype.XMLGregorianCalendar;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
XMLGregorianCalendar xmlDate = new XMLGregorianCalendarImpl();
xmlDate.setMonth(12);
xmlDate.setDay(31);
xmlDate.setYear(2019);
xmlDate.setHour(0);
xmlDate.setMinute(0);
xmlDate.setSecond(0);
Calendar calendar = xmlDate.toGregorianCalendar();
SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
Date dt = calendar.getTime();
String ds1 = dt.toString();
System.out.println("ds1 = " + ds1);
String dateString = formatter.format(calendar.getTime());
System.out.println("dateString = " + dateString );
}
}
I cannot figure out why the year component of dateString is showing as 2020 instead of 2019.
ds1 = Tue Dec 31 00:00:00 EST 2019
dateString = 2020-12-31 00:00:00
Please change your code to
import javax.xml.datatype.XMLGregorianCalendar;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
XMLGregorianCalendar xmlDate = new XMLGregorianCalendarImpl();
xmlDate.setMonth(12);
xmlDate.setDay(31);
xmlDate.setYear(2019);
xmlDate.setHour(0);
xmlDate.setMinute(0);
xmlDate.setSecond(0);
Calendar calendar = xmlDate.toGregorianCalendar();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt = calendar.getTime();
String ds1 = dt.toString();
System.out.println("ds1 = " + ds1);
String dateString = formatter.format(calendar.getTime());
System.out.println("dateString = " + dateString );
}
}
As YYYY represents year of the week and yyyy represents calendar year in simple date format.
More explanation here: Java SimpleDateFormat shifts Date by one year
This question already has answers here:
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
return date type with format in java [duplicate]
(1 answer)
Closed 4 years ago.
I want to take a date value as String in the format yyyy-MM-dd and return a jabva util date in the same format.
for this I am using below code , but the result is coming as "Wed Sep 18 00:00:00 CEST 2013"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String strdate = "2013-09-18";
Date utilDate = sdf.parse(strdate);
System.out.println(utilDate);
Do i need to consider the locale also ?
Please help to achieve this.
Thanks in advance
Please try below code.
You also have to format your simpleDateFormat object and pass utilDate object inside it to create formatted date as specified by you.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateTest {
public static void main(String[] args) throws ParseException{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String strdate = "2013-09-18";
Date utilDate = sdf.parse(strdate);
String date=sdf.format(utilDate );
System.out.println(date);
}
}
We can also implement through Java8 like below:
import java.text.ParseException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateTest {
public static void main(String[] args) throws ParseException{
String strdate = "2013-09-18";
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.parse(strdate, formatter);
System.out.println(date.format(formatter));
}
}
or you can also use only LocalDate to change in java util like below.
import java.time.LocalDate;
public class TestCircle {
public static void main(String args[]){
String strdate = "2013-09-18";
LocalDate date = LocalDate.parse(strdate);
System.out.println(date);
}
}
This question already has answers here:
Convert String Date to String date different format [duplicate]
(8 answers)
Closed 5 years ago.
Can anyone tell me how to convert this dateString "2003Jan01" into another dateString which is in this format "01-JAN-03"?
One way is using two SimpleDateFormat: one to parse the input, one to format the output:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class SDF
{
public static void main(String args[])
{
SimpleDateFormat source = new SimpleDateFormat("yyyyMMMdd", Locale.ENGLISH);
try
{
Date date = source.parse("2003Jan01");
SimpleDateFormat destination = new SimpleDateFormat("dd-MMM-yy", Locale.ENGLISH);
System.out.println(destination.format(date));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
This question already has answers here:
How to convert String object to Date object? [duplicate]
(8 answers)
Closed 9 years ago.
How can this 11/03/2009-20:06:16 to a Date object so I can compare two dates. I keep getting java.lang.IllegalArgumentException: Cannot format given Object as a Date error if I use below implementation.. I need the output to be date object with format Tue Jul 14 01:32:31 2009
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Dateaf {
/**
* #param args
* #throws ParseException
*/
public static void main(String[] args) throws ParseException {
// TODO Auto-generated method stub
String text = "11/03/2009-20:06:16";
SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy-hh:mm:ss");
Date date = dateParser.parse(text);
System.out.println(date);
}
}
sdf.format() takes a Date object and returns a String. You are passing a String to it.
You are very close, just drop the call to format:
Date date = sdf.parse(text);
So:
String text = "11-03-2009 20:06:16";
SimpleDateFormat dateParser = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
//^^ capital H for 24 hour
Date date = dateParser.parse(text);
And to print (from your comment in the format "Tue Jul 14 01:32:31 2009"):
SimpleDateFormat dateFormatter = new SimpleDateFormat("EE MM dd HH:mm:ss yyyy");
System.out.println(dateFormatter.format(date));
The SimpleDateFormat javadoc is an excellent reference for formats.
EDIT
After OP's edits here is a full example
public static void main(String[] args) throws ParseException {
final String text = "11/03/2009-20:06:16";
final SimpleDateFormat dateParser = new SimpleDateFormat("MM/dd/yyyy-HH:mm:ss");
final Date date = dateParser.parse(text);
final SimpleDateFormat dateFormatter = new SimpleDateFormat("EE MMM dd HH:mm:ss yyyy");
System.out.println(dateFormatter.format(date));
}
Output:
Tue Nov 03 20:06:16 2009
Java has the text package which gives us the predefined class text
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) throws Exception{
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date today = df.parse("20/12/2005");
System.out.println("Today = " + df.format(today));
}
}
There is another way of doing this
String date = "2000-11-01";
java.sql.Date javaSqlDate = java.sql.Date.valueOf(date);