debosmebo
debosmebo

Reputation: 13

SimpleDateFormat returning the wrong date in Java

I'm having an issue with SimpleDateFormat in Java. My code is returning the wrong date. Help please.

String date_str = "Tue Mar 08 09:44:55 EST 2022";
  
Date date = new SimpleDateFormat("EEE MMM D HH:mm:ss z yyyy").parse(date_str);

// Output: Sat Jan 08 14:44:55 GMT 2022

Upvotes: 1

Views: 86

Answers (1)

Federico klez Culloca
Federico klez Culloca

Reputation: 27119

d, not D for the day of the month. D is the day in the year, so 8 is the 8 of January.

String date_str = "Tue Mar 08 09:44:55 EST 2022";
  
Date date = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy").parse(date_str);
                                       // ^---Here

// Output: Tue Mar 08 15:44:55 CET 2022

Check the full list of patterns here.

Upvotes: 3

Related Questions