Convert into format YYYY-MM-DD [duplicate] - java

This question already has answers here:
Java string to date conversion
(17 answers)
Closed 6 years ago.
I want this gmt time into YYYY-MM-DD format but i went through the procedure it give parse exception error.
I want to convert this String type of Fri Apr 08 2016 00:00:00 GMT+0530 (IST) into date format so that i can insert into my mqysql data base.

If you want to get "YYYY-MM-DD" formate of mysql date format this code could help.
DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("YYYY-MM-dd");
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("EEE MMM dd yyyy hh:mm:ss zZ (z)");
TemporalAccessor parse = formatter.parse("Fri Apr 08 2016 00:00:00 GMT+0530 (IST)");
LocalDate localDate=LocalDate.from(parse);
String formated= dateTimeFormatter.format(localDate);
System.out.println("formated:"+formated);

Related

Wrong output with Java SimpleDateFormat [duplicate]

This question already has answers here:
SimpleDateFormat producing wrong date time when parsing "YYYY-MM-dd HH:mm"
(5 answers)
"EEE MMM dd HH:mm:ss ZZZ yyyy" date format to java.sql.Date
(2 answers)
Java Date() giving the wrong date [duplicate]
(9 answers)
Closed 1 year ago.
I need to parse a date from this format: WeekDay Month DD HH:MM:SS GFT YYYY to this format: YYY/MM/DD, and this is what I tried to do:
SimpleDateFormat format = new SimpleDateFormat("YYYY/MM/DD");
String conclusionDate = format.format(filter.getConclusionDate());
But there is something wrong going on. For example, when I try to format this date: "Thu Sep 01 00:00:00 GFT 2016", what I get is "2016/09/245" instead of "2016/09/01"
The weirdest is that it seems to be happening only with months different than JANUARY. When I try another random example like "Sat Jan 01 00:00:00 GFT 2000" I get exactly what I want: " 2000/01/01 ". It still formats correctly if I vary the day field
But when I change it to be another month, like february (" Tue Feb 01 00:00:00 GFT 2000 "), I get it wrong: " 2000/02/32 "
Why is it?
OBS: Before you ask: Yes, I have checked and the date that is returned in getConclusionDate() is exactly in the expected format WeekDay Month DD HH:MM:SS GFT YYYY .
D is the day in the year, d is the day of the month. You want YYY/dd/MM https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Unable to parse some dates into ZonedDateTime [duplicate]

This question already has answers here:
java DateTimeFormatterBuilder fails on testtime [duplicate]
(2 answers)
Closed 1 year ago.
Trying to parse incoming dates from a third-party source, some dates work as expected others throw an exception:
Text 'Fri, 11 Jun 2021 02:25:23 +0000' could not be parsed at index 8
Looking at the dates I can't spot a difference in them, and looking at my formatter I can't see where I've gone wrong.
Example failing date: Fri, 11 Jun 2021 02:25:23 +0000
Example passing date: Sun, 30 May 2021 11:42:03 +0000
The code I'm using to parse the dates:
ZonedDateTime.parse(incomingDate, DateTimeFormatter.ofPattern("E, d MMM yyyy HH:mm:ss Z"));
The only thing I can think that some date months are shorthand vs others that are not (May vs Jun for example).
Would love some help.
String incomingDate1 = "Fri, 11 Jun 2021 02:25:23 +0000";
String incomingDate2 = "Sun, 30 May 2021 11:42:03 +0000";
ZonedDateTime parsed = ZonedDateTime.parse(incomingDate1,
DateTimeFormatter.ofPattern("E, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH));
System.out.println(parsed);

Java convert string to date not converting correctly [duplicate]

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

How to parse TZ format time with non-GMT time zone [duplicate]

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

How can i set the calender instance getTime value back to the date object? [duplicate]

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)

Categories