0

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 .

  • *For example, when I try to format this date: "Thu Sep 01 00:00:00 GFT 2016"* You'd need *parse* that first. How is that done? – g00se Aug 11 '21 at 18:07
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 11 '21 at 18:24

1 Answers1

2

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

Duston
  • 1,601
  • 5
  • 13
  • 24
  • It’s still not correct. And you don’t want to use `SimpleDateFormat` at all (for how it’s not correct see [Why does sdf.format(date) converts 2018-12-30 to 2019-12-30 in java? \[duplicate\]](https://stackoverflow.com/questions/57620110/why-does-sdf-formatdate-converts-2018-12-30-to-2019-12-30-in-java)). – Ole V.V. Aug 12 '21 at 05:55