Solved

Java SimpleDateFormat parse method returns incorrect date

Posted on 2011-09-29
6
301 Views
Last Modified: 2012-05-12
I am using SimpleDateFormat to return the date equivalent of a string for example the date format MM/dd/yyyy and the constructed string '09/31/2011' when passed to the parse method gives me back a date object equivalent to 10/01/2011 which is incorrect - any ideas why this happens ?
0
Comment
Question by:abuyusuf35
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 47

Accepted Solution

by:
for_yan earned 250 total points
ID: 36817356
There cannot be 09/31/2011 as there are only 30 days in september
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36817360
So it treas it as the next day which is 10.01/2011
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36817616


well it deos it with all un-realistic dates; I'd think it should throw an exception, but it does not:

I just checked (look how it makes mar 7th form feb 35th):
        SimpleDateFormat sdf3 = new SimpleDateFormat("MM/dd/yyyy");
        System.out.println("sdf3 prsed. sep 31st: " + sdf3.parse("09/31/2011",new ParsePosition(0)));


        System.out.println("sdf3 prsed, oct 31st: " + sdf3.parse("10/31/2011",new ParsePosition(0)));

           System.out.println("sdf3 prsed, jun 31st: " + sdf3.parse("06/31/2011",new ParsePosition(0)));

            System.out.println("sdf3 prsed, jul 31st: " + sdf3.parse("07/31/2011",new ParsePosition(0)));

            System.out.println("sdf3 prsed, feb 31st: " + sdf3.parse("02/31/2011",new ParsePosition(0)));

                  System.out.println("sdf3 prsed, feb 35th: " + sdf3.parse("02/35/2011",new ParsePosition(0)));

Open in new window


Output:

sdf3 prsed. sep 31st: Sat Oct 01 00:00:00 PDT 2011
sdf3 prsed, oct 31st: Mon Oct 31 00:00:00 PDT 2011
sdf3 prsed, jun 31st: Fri Jul 01 00:00:00 PDT 2011
sdf3 prsed, jul 31st: Sun Jul 31 00:00:00 PDT 2011
sdf3 prsed, feb 31st: Thu Mar 03 00:00:00 PST 2011
sdf3 prsed, feb 35th: Mon Mar 07 00:00:00 PST 2011

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 36817652

And even more "progressive" Calendar does the same thing
        GregorianCalendar gc = new GregorianCalendar(2011, 8, 30);

         System.out.println("gereg cal . sep 30th: " + gc.getTime());

        gc = new GregorianCalendar(2011, 8, 31);

        System.out.println("gereg cal . sep 31th: " + gc.getTime());

        gc = new GregorianCalendar(2011, 1, 35);

          System.out.println("gereg cal . feb 35th: " + gc.getTime());






        SimpleDateFormat sdf3 = new SimpleDateFormat("MM/dd/yyyy");
        System.out.println("sdf3 prsed. sep 31st: " + sdf3.parse("09/31/2011",new ParsePosition(0)));


        System.out.println("sdf3 prsed, oct 31st: " + sdf3.parse("10/31/2011",new ParsePosition(0)));

           System.out.println("sdf3 prsed, jun 31st: " + sdf3.parse("06/31/2011",new ParsePosition(0)));

            System.out.println("sdf3 prsed, jul 31st: " + sdf3.parse("07/31/2011",new ParsePosition(0)));

            System.out.println("sdf3 prsed, feb 31st: " + sdf3.parse("02/31/2011",new ParsePosition(0)));

                  System.out.println("sdf3 prsed, feb 35th: " + sdf3.parse("02/35/2011",new ParsePosition(0)));

Open in new window



gereg cal . sep 30th: Fri Sep 30 00:00:00 PDT 2011
gereg cal . sep 31th: Sat Oct 01 00:00:00 PDT 2011
gereg cal . feb 35th: Mon Mar 07 00:00:00 PST 2011

sdf3 prsed. sep 31st: Sat Oct 01 00:00:00 PDT 2011
sdf3 prsed, oct 31st: Mon Oct 31 00:00:00 PDT 2011
sdf3 prsed, jun 31st: Fri Jul 01 00:00:00 PDT 2011
sdf3 prsed, jul 31st: Sun Jul 31 00:00:00 PDT 2011
sdf3 prsed, feb 31st: Thu Mar 03 00:00:00 PST 2011
sdf3 prsed, feb 35th: Mon Mar 07 00:00:00 PST 2011

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36817721
There's a simple solution to that:
sdf3.setLenient(false); // returns null

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36817740
And it will throw an exception if you follow that will the less verbose
sdf3.parse("09/31/2011");

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question