• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

No exception when parsing date from string

 
Author
Posts: 131
7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I think I should be getting an exception here but i'm not. I have:
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", user.getLocale());
When I try to parse an *invalid* date I expected an exception to be thrown:
sdf.parse("99/99/9999");
I am NOT getting an exception and Java returns a java.util.Date object with the toString value:
Thu Jun 07 00:00:00 CDT 10007
Shouldn't the parse() method have thrown an exception? How do I get it to?
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think you should get an exception? According to the API the only exception thrown by parse is a NullPointerException, and your invalid date certainly was not null.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
toggle on/off the sdf.setLenient(false) to see what happens
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
parse is still not throwing an exception. With leinent off, parse is returning null, and setTime is throwing a NullPointerException.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to the API the only exception thrown by parse is a NullPointerException, and your invalid date certainly was not null.
That's true for parse(String, ParsePosition) - but not parse(String) which is defined in DateFormat and not overridden. Michael, could you show the code you're using to parse this?
 
Michael Remijan
Author
Posts: 131
7
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael Dunn's tip worked for me, thanks Michael. When I set lienient to false, the parsing would not allow dates like 99/99/9999 or 4/40/2004.
If lienient is not working for you, search through your code for "new SimpleDateFormat" and ".getInstance". The first time I tried setting lienient to false did not work because I missed a place in my code where I create a DateFormat object with ".getInstance"
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic