Wrong output with Java SimpleDateFormat [duplicate] - java

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

Related

DateTimeFormatter throwing exception "could not be parsed at index 0" [duplicate]

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)

java.time.format.DateTimeParseException: Text 'Tue Jan 08 00:00:00 IST 2019' could not be parsed at index 0 [duplicate]

This question already has answers here:
java DateTimeFormatterBuilder fails on testtime [duplicate]
(2 answers)
How can I convert Date.toString back to Date?
(5 answers)
Java - Unparseable date
(3 answers)
Closed 4 years ago.
I am facing DateTimeParseException even after giving appropriate format
DateTimeFormatter ft = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy");
LocalDateTime.parse("Tue Jan 08 00:00:00 IST 2019", ft);
Please help if I am missing anything?
This is probably due to the locale setting on your computer.
You might provide a Locale when creating the DateTimeFormatter.
DateTimeFormatter ft = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
This will ensure that the date is always parsed right.
This could be caused by your system locale not using Mon-Sun for week day short names, e.g. same exception will be thrown for German locale:
Locale.setDefault(Locale.GERMAN);
DateTimeFormatter ft = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy");
LocalDateTime.parse("Tue Jan 08 00:00:00 IST 2019", ft);
The code will work if you use the matching locale e.g. US:
Locale.setDefault(Locale.US);
DateTimeFormatter ft = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z yyyy");
LocalDateTime.parse("Tue Jan 08 00:00:00 IST 2019", ft);

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)

Convert into format YYYY-MM-DD [duplicate]

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);

How to convert this "Tue Nov 13 14:35:04 +0000 2012" String format to date in Java?

How to convert the date Tue Nov 13 14:35:04 +0000 2012 String format to date in Java?
I know of Date.parse(String) but I don't know which format I should use for the date. Do I have to modify the string so that it can be parsed into date, and if yes then how?
Use SimpleDateFormat, with a format string of
"E MMM dd HH:mm:ss Z yyyy"
You should explicitly use Locale.US assuming these will definitely use English month/day names. (You don't want to be trying to parse French names just because the default locale is French, for example.)
Also, don't forget that the Date value returned will have no knowledge of the original time zone - it will have the right value for the instant represented in the original text, but don't expect the result of calling toString() to use the same zone - Date.toString() always uses the default time zone.
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
Date test = sdf.parse("Tue Nov 13 14:35:04 +0000 2012");
System.out.print(test.toString());

Categories