• 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

SimpleDateFormat's parse method behavior

 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Ranchers,


In above function as you can see in input variable date given is incorrect. but when I call this method this function run perfectly fine and gives below output,
Sat Jan 01 00:00:00 IST 1

I dont understand how can I validate date so formatter.parse() method will return null as given date is invalid?

Thanks in advance ...
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The documentation of the DateFormat#parse() method clearly says that the complete input string might not be processed. So, from your "input" string only, "01.01.1" portion is parsed, and since it satisfies the pattern - "dd.MM.yyyy", it will not give you "null".

If you want to return "null" from the method, if the date is not valid as per the pattern, then you can check the index of the ParsePosition. If it is at the end, then it has parsed complete string, and hence date is valid, else date is invalid, and return null:

Some Points:

  • I've changed the return type of method to "Date", as it's better to return the parsed date from the method, rather than printing it there only.
  • Also, it's better to pass the "input" and "pattern" string as argument to this method. That way, you can re-use this method.
  • Also, I would rather throw an exception, in case of invalid date, rather than returning "null".
  • And, the method is better named as "parseDate" than "dateParse".


  •  
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    R. Jain wrote: . . . only, "01.01.1" portion is parsed, . . .

    I never realised that. Useful answer
     
    Shailesh Narkhede
    Ranch Hand
    Posts: 368
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks R Jain....
     
    R. Jain
    Ranch Hand
    Posts: 375
    1
    Python Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Shailesh Narkhede wrote:Thanks R Jain....


    You're welcome
     
    Sheriff
    Posts: 22783
    131
    Eclipse IDE Spring VI Editor Chrome Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    R. Jain wrote:


    Shouldn't that be pos.getIndex() < input.length(), without the - 1? Because after the parsing pos.getIndex() will refer to the first character after the parsed string. If the parsing consumes the entire string, at the end pos.getIndex() will be equal to input.length().
     
    R. Jain
    Ranch Hand
    Posts: 375
    1
    Python Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rob Spoor wrote:Shouldn't that be pos.getIndex() < input.length(), without the - 1?


    Oops, right. Will edit my answer. Thanks
     
    reply
      Bookmark Topic Watch Topic
    • New Topic