Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java date parse missbehaviour [duplicate]

Tags:

java

date

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, YYYY, EEE", Locale.US);

System.out.println(sdf.format(new Date()));
System.out.println(sdf.format(sdf.parse("Apr 27, 2018, Fri")));

Java does not parse the date as expected and outputs:

Apr 27, 2018, Fri
Jan 05, 2018, Fri // I can not understand why java parse the month of April as January
like image 903
user9709261 Avatar asked Apr 27 '18 08:04

user9709261


1 Answers

Your problem is that you are using YYYY, which represents Week year, whereas you wanted yyyy which represents year, see all options here.

SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy, EEE", Locale.US);
like image 93
achAmháin Avatar answered Sep 21 '22 21:09

achAmháin