Solved

Java SimpleDateFormat parse method returns incorrect date

Posted on 2011-09-29
6
299 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
  • 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

828 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