-3

i have this code

public class HelloWorld{

 public static void main(String []args){
     try{
        java.util.Date mDate;
        java.text.SimpleDateFormat readFormat = new java.text.SimpleDateFormat("yyyy-mm-dd");
        mDate = readFormat.parse("2017-02-05");
        System.out.println("date: " + mDate.toString());
     } catch (java.text.ParseException e1) {
            e1.printStackTrace();
        }
     }

}

i expected output like Feb 05 00:02:00 UTC 2017

but im getting output Jan 05 00:02:00 UTC 2017 why? what's wrong in my code?

enter image description here

1 Answers1

5

When you create this format: new java.text.SimpleDateFormat("yyyy-mm-dd"), that means

  1. 4-digit year
  2. 2-digit minute in hour
  3. 2-digit day in month

So "2017-02-05" is two minutes into the 5th day of 2017, and that's 5 of january.

You should use new java.text.SimpleDateFormat("yyyy-MM-dd"). See the documentation.

Axel
  • 13,939
  • 5
  • 50
  • 79