This question already has answers here:
Unable to parse date Apr 15, 2020 12:14:17 AM to LocalDatetime
(2 answers)
SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"
(5 answers)
simpledate formater to format returns incorrect.deducted date after formating [duplicate]
(2 answers)
Java Date() giving the wrong date [duplicate]
(9 answers)
Java SimpleDateFormat always returning January for Month
(4 answers)
Closed 2 years ago.
I have the following:
Date dateCommence = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss").parse("2021-01-06 00:00:00");
But the dateCommence is the following:
Sun Dec 27 00:00:00 SAST 2020
Question
How do I convert the "2021-01-06 00:00:00" string to a date?
You were using the wrong date format mask. From the documentation, Y corresponds to the week year, and D is the day in year.
Try this version:
Date dateCommence = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse("2021-01-06 00:00:00");
System.out.println(dateCommence);
This prints:
Wed Jan 06 00:00:00 CET 2021
Related
This question already has answers here:
Java - Unparseable date
(3 answers)
DateTimeFormatter month pattern letter "L" fails
(3 answers)
Closed 2 years ago.
I keep on getting an error from running this code.
java.time.format.DateTimeParseException: Text 'Jan 03, 2020' could not be parsed at index 0
final String myFormat = "LLL dd, yyyy"; //sets format in which to show date (same as how its saved in database) ex. Jan 29, 2020
final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(myFormat);
String startingBiWeeklyCheck = sharedPreferences.getString("biweekly start", "Jan 03, 2020");
LocalDate startingDate = LocalDate.parse(startingBiWeeklyCheck, dateFormatter);
Ive played around with the format but I'm not seeing why the pattern "LLL dd, yyyy" doesn't parse Jan 03, 2020
You should use MMM instead of LLL for month parsing.
Updated:
I was wrong about my answer above. It's the half of answer.
The deal is DateTimeFormatter.ofPattern(myFormat) uses default Locale.
For non-US locales, it doesn't work.
So you need to specify the locale according to your pattern.
DateTimeFormatter.ofPattern(myFormat).withLocale(Locale.US)
This question already has answers here:
Java string to date conversion
(17 answers)
Closed 2 years ago.
I have timestamp of the format 2020-05-12 12:00:00+00 . How can I parse into Java.util.Date and Java.time.Instant.
Seemingly basic question, but I suspect it is the source of the problem that I am yet to solve in thread
Java 8 introduced DateTimeFormatter. Here is the link to the DateTimeFormatter Documentation.
For example:
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ssx");
String tsFromDb = "2020-05-12 12:00:00+00";
Instant inst = formatter.parse(tsFromDb, Instant::from);
System.out.println(inst);
Output:
2020-05-12T12:00:00Z
If you need a java.util.Date:
Date oldfashionedDate = Date.from(inst);
System.out.println(oldfashionedDate);
Output in America/Tijuana time zone:
Tue May 12 05:00:00 PDT 2020
This question already has answers here:
Difference between 'yy' and 'YY' in Java Time Pattern [duplicate]
(3 answers)
Y returns 2012 while y returns 2011 in SimpleDateFormat
(5 answers)
Closed 3 years ago.
Please take a look at the following code:
Date date = new GregorianCalendar (2019, Calendar.DECEMBER, 30).getTime ();
System.out.println (date);
SimpleDateFormat formatter = new SimpleDateFormat ("dd.MM.YYYY");
System.out.println (formatter.format (date));
The output is as follow:
Mon Dec 30 00:00:00 CET 2019
30.12.2020
Look at the year. What is wrong?
Same problem with '2019-12-31' while '2019-12-29' is ok.
Best regards
Thomas
This question already has answers here:
Parse ISO8601 date string to date with UTC Timezone
(4 answers)
Converting ISO 8601-compliant String to java.util.Date
(31 answers)
Parsing ISO-8601 DateTime with offset with colon in Java
(4 answers)
Closed 3 years ago.
I have time string in two formats.
2019-03-01T22:22:50.591-08:00
2019-03-01T22:22:50.591Z
I am using following Java snippet to parse
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:SS" +
".SSS'Z'", Locale.US);
Date date = df.parse(dateString);
Its working on second string but failing on first. What common code can be there to parse both of them?
This might be what you're looking for: "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
Date parsed1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US).parse("2019-03-01T22:22:50.591Z");
System.out.println(parsed1); // Fri Mar 01 16:22:50 CST 2019
Date parsed2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US).parse("2019-03-01T22:22:50.591-08:00");
System.out.println(parsed2); // Sat Mar 02 00:22:50 CST 2019
As Basil points out below, these also work if you are running Java 8+ or a backport of JSR 310:
OffsetDateTime parsed1 = OffsetDateTime.parse("2019-03-01T22:22:50.591Z");
System.out.println(parsed1); // Fri Mar 01 16:22:50 CST 2019
OffsetDateTime parsed2 = OffsetDateTime.parse("2019-03-01T22:22:50.591-08:00");
System.out.println(parsed2); // Sat Mar 02 00:22:50 CST 2019
This question already has answers here:
Convert String to Calendar Object in Java
(8 answers)
How to parse a date? [duplicate]
(5 answers)
Closed 5 years ago.
First i have written the Calender instance getTime method value to a file. now by reading the file i want to set that value as date object?
Format of the data written to the file
Wed Mar 29 18:54:53 IST 2017
How can i do that?
Thanks!!
Check out the the class SimpleDateFormat: https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
This should work for your example:
String inputString = "Wed Mar 29 18:54:53 IST 2017";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
sdf.parse(inputString)