-1

Any idea what is happening to this code that the dates do not coincide? First prints "20 December 2017" and then "Date with new format:2017-01-01"...

SimpleDateFormat formatter = new SimpleDateFormat("d MMMM YYYY");
SimpleDateFormat formatterNew = new SimpleDateFormat("yyyy-MM-dd");
try{
    System.out.println(formatter.format(new Date()));
    Date date = formatter.parse("20 December 2017");
    System.out.println("Date with new format:" +formatterNew.format(date));            
}catch(Exception ex){
    ex.printStackTrace();
}
Gorka
  • 377
  • 3
  • 14

1 Answers1

1

This is related to the use of yyyy vs YYYY. If you change your code to use yyyy it will work.

Here is the explanation of the difference between the two.

Week year

AdamPillingTech
  • 456
  • 2
  • 7