Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java SimpleDateFormat parse method returns incorrect date

Posted on 2011-09-29
6
Medium Priority
?
306 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

963 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