• Post Reply Bookmark Topic Watch Topic
  • New Topic

SimpleDateFormat's parse method behavior  RSS feed

 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • 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 ...
 
R. Jain
Ranch Hand
Posts: 375
1
Java Python Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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".


  •  
    Campbell Ritchie
    Marshal
    Posts: 56536
    172
    • Mark post as helpful
    • send pies
    • 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
    • Quote
    • Report post to moderator
    Thanks R Jain....
     
    R. Jain
    Ranch Hand
    Posts: 375
    1
    Java Python Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Shailesh Narkhede wrote:Thanks R Jain....

    You're welcome
     
    Rob Spoor
    Sheriff
    Posts: 21135
    87
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • 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
    Java Python Ubuntu
    • Mark post as helpful
    • send pies
    • 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
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!