Solved

Java SimpleDateFormat parse method returns incorrect date

Posted on 2011-09-29
6
289 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
Comment Utility
There cannot be 09/31/2011 as there are only 30 days in september
0
 
LVL 47

Expert Comment

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

Expert Comment

by:for_yan
Comment Utility


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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 47

Expert Comment

by:for_yan
Comment Utility

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
Comment Utility
There's a simple solution to that:
sdf3.setLenient(false); // returns null

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scoreUp challenge 14 47
mapAB Challlenge 35 84
Systems talking to each other 5 107
get weblogic logged in user in java 2 39
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now