Sharfi Sharfi - 1 month ago 6
Java Question

I am getting an unexpected output of SimpleDateFormat.parse().

When I am executing the following code

SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/YYYY");

Date today=new Date();

String olddate="03/11/2016";
System.out.println(sdf.parse(olddate));


Why the Result is displaying Sun Dec 27 00:00:00 IST 2015.

However I was expecting Wed Nov 03 00:00:00 IST 2016.

Anyone can please explain. And how can I get the desired output.

Answer

Date format is wrong. It should be y instead of Y-

dd/MM/yyyy

  • Y specify Week year
  • y specify Year

For a more detailed insight view, check the link Oracle Docs and you'll see the meaning of each character we pass there.

Comments