This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Parsing date using SimpleDateFormat for hh mm ss Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parsing date using SimpleDateFormat for hh mm ss" Watch "Parsing date using SimpleDateFormat for hh mm ss" New topic
Author

Parsing date using SimpleDateFormat for hh mm ss

Gaurav Kr. Arora
Ranch Hand

Joined: Feb 20, 2011
Posts: 37
Hi,

My program is shown below:


In this program if my input date string is "12/03/1956 12:34:99", then also I am getting valid date in sysout.
Output: Valid new date is :Mon Dec 03 12:34:00 IST 1956

My requirement is to show date invalid to user if in input seconds are supplied. My input is valid if it is 12/03/1956 12:34, else invalid if it contains seconds information.
How can I achieve it without using substrings, regex? I can only use SimpleDateFormat.

Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

First of all, you don't need to re-format the Date to show only the year and then parse that to an int. You can use a Calendar for that:
Now, to the problem at hand. This is one of the reasons I don't like the parse(String) methods. They allow trailing characters after the valid input. By using the other parse method, parse(String, ParsePosition), you can prevent this. An added benefit is that you don't need to catch a ParseException:
The parse(String) method only checks for validity. This also checks if the current parse position is directly at the end of the input. If it isn't then there are characters after the valid date.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Gaurav Kr. Arora
Ranch Hand

Joined: Feb 20, 2011
Posts: 37
Thanks for the timely help! :-)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

You're welcome.
Gaurav Kr. Arora
Ranch Hand

Joined: Feb 20, 2011
Posts: 37
One more doubt.

I have string like "January2011" which can be validated using "MMMMMyyyy" pattern. But, "MMMMMyyyy" pattern will validate "Jan2011" which I don't want.
I can use parse position and the way suggested in above posts, but since my code is generic and am validating other patterns also, I can't do it.

So, is it possible to validate fullmonthYYYY with any pattern in java without using parse position and checking the length of pattern?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parsing date using SimpleDateFormat for hh mm ss
 
Similar Threads
simpleDateFormat Problems and questions
Code review required for converting dateformat
Validating date using regex and simple date format
Date Formatting.
Date; Previous date, Calendar; Gregorian