Solved

Java SimpleDateFormat parse method returns incorrect date

Posted on 2011-09-29
6
302 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

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!

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

630 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