Strange date effect


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Strange date effect

  1. #1
    Join Date
    Mar 2005
    Location
    London
    Posts
    18

    Strange date effect

    Hi there.

    I'm retrieving a string from a webform and processing it in a servlet. Testing has revealed a rather odd effect; if a supposedly invalid date such as "01/14/2005" is input the code accepts this as 01 Feb 2006. Equally "01/17/2005" is accepted as 01 May 2006.

    The java seems to be just adding the any extra months after 12 into subsequent years. This is not going to pass testing!

    Here's the code:
    Code:
    java.util.Date datDate = datJava.parse(request.getParameter("donordate"));
    this.strDonDate = datSyb.format(datDate);
    System.out.println(strDonDate);
    Output of this is as above.

    Anyone seen this effect before? And know how to get around it?

    Thanks, DaveMere

  2. #2
    Join Date
    Oct 2004
    Posts
    311
    Why don't you just use a SimpleDateFormatter? This formatter should throw the correct exception in the case you stated above.

  3. #3
    Join Date
    Mar 2005
    Location
    London
    Posts
    18
    Thanks for your reply ractoc. I didn't include the rest of the code! Here it is;

    Code:
    SimpleDateFormat datJava = new SimpleDateFormat( "dd/MM/yyyy" );
    SimpleDateFormat datSyb = new SimpleDateFormat( "MMM dd yyyy" );
    
    java.util.Date datDate = datJava.parse(request.getParameter("donordate"));
    this.strDonDate = datSyb.format(datDate);
    System.out.println(strDonDate);
    So, am I not already using SimpleDateFormatters? Or have I made a mistake somewhere?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center