2

I have this simple example:

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/YY");
        System.out.println( sdf.format(new Date()));
        try {
            System.out.println(  sdf.parse("10/18/14")  );
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

In the console, I'm getting 10/18/14 Sun Dec 29 00:00:00 EST 2013

Any explanation why sdf.parse return dec 29th 2013 in stead of Oct 18th 2014

Adnane
  • 21
  • 1

2 Answers2

4

Your format String is YY and should be yy

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

Per the SimpleDateFormat Javadoc (which says in part),

Letter    Date or Time Component  Presentation    Examples
G         Era designator          Text            AD
y         Year                    Year            1996; 96
Y         Week year               Year            2009; 09

Currently you're getting the Week year.

When I make the above change I get (the expected)

10/18/14
Sat Oct 18 00:00:00 EDT 2014
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Same thing happend here : SimpleDateFormat appears to give an incorrect result Try to look if someone had the same issue first, it's easier and faster to get an answer for your problem.

Community
  • 1
  • 1
Tahar Bakir
  • 716
  • 5
  • 14