-2

Why the date printed is 00 instead of 01?

    DateFormat myFormatter = new SimpleDateFormat("dd/MM/yyyy");
    Date newDate = myFormatter.parse("01/02/2016"); //assume date today is Feb 01, 2016
    System.out.println(myFormatter.format(newDate));

Date printed in the console is: 00/02/2016

But what i wanted to have is: 01/02/2016

Please can someone explain what could be gone wrong in my code? And how I can fix it?

P.S: February in 2016 having 29 days

Melchizedek
  • 1,057
  • 17
  • 29
Yithirash
  • 377
  • 3
  • 6
  • 18
  • 1
    Are you sure? I can't reproduce your issue, and I can't understand how this might happen. – Dawood ibn Kareem Aug 22 '19 at 03:50
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 22 '19 at 05:24
  • 1
    *Either* your problem is elsewhere. *Or* it’s a multithreading issue. The old-fashioned `SimpleDateFormat` is not thread-safe, so using it from two or more different threads could easily give this error occassionally. Possible duplicate of [Simple Date format returns Wrong date intermittently \[duplicate\]](https://stackoverflow.com/questions/52000493/simple-date-format-returns-wrong-date-intermittently). – Ole V.V. Aug 22 '19 at 05:46
  • @OleV.V. yes, its a multi-threaded program. the date variable is passed to 4 other methods. The last three methods showing correct output. but only the first method giving incorrect output. I will try to use DateTimeFormatter. – Yithirash Aug 22 '19 at 06:59
  • I have retracted my vote to close as unreproducible. Now I am not allowed to vote to close as a duplicate of said question, so if others would please do that? – Ole V.V. Aug 22 '19 at 08:45
  • @Yithirash - that's the kind of detail that you should include in the question - it helps people work out the answer if they know that your program is multi-threaded, and that the problem doesn't happen every time this code is run. One solution then, is to use a different `SimpleDateFormat` object in each thread. Another, as others have suggested, is to use `DateTimeFormatter` instead. – Dawood ibn Kareem Aug 22 '19 at 20:06

1 Answers1

0

First things first, adjusting your existing code to print the parsed version of your input here

System.out.println(myFormatter.format(myFormatter.parse("01/02/2016")));

prints "01/02/2016" here; check the way you are generating the Date. You could also try

DateFormat myFormatter = new SimpleDateFormat("dd/MM/yyyy", Locale.US);

because there is no telling what Locale your system has.

Finally, and arguably one should always do this first, try a debugger.

P.S. In new code you should prefer LocalDate and DateTimeFormatter. The Java time api is far more consistent.

P.P.S. Not certain why it is relevant, but you are correct that February 2016 has 29 days.

$ cal 2 2016

  February 2016
Su Mo Tu We Th Fr Sa
    1  2  3  4  5  6
 7  8  9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249