java date parse missbehaviour

  • A+
Category:Languages
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 


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

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: