4

In the below code i need to get a parse exception.but the program somehow converts it to a valid date.

But if i give dthours as "07:0567" it is giving parse error.So how to keep the exact format shown.

Can anyone tell me what to do to throw an error if the date string deviates from the given format ("HH:MM:SS") even by a single character.

public static void main(String[] args) {

    String dthours="07:4856:35563333";
    SimpleDateFormat df = new SimpleDateFormat("HH:MM:SS"); 
    try
    {
        Date d = df.parse(dthours);
        System.out.println("d "+d);
    }
    catch (ParseException e)
    {
        System.out.println("parseError");

    }
ashwinsakthi
  • 1,856
  • 4
  • 27
  • 56
  • You want to parse 07 hours, 4856 months and 35563333 milli seconds. (no days, minutes or seconds) Perhaps your clock is broken? Perhaps you wanted "HH:mm:ss.SSS" which would look like `07:48:35.563` – Peter Lawrey Aug 22 '12 at 11:56

1 Answers1

10

Set the df.setLenient() to false so that the SimpleDateFormat will throw parse exception in such cases.

public static void main(String[] args)
{
    String dthours = "07:4856:35563333";
    SimpleDateFormat df = new SimpleDateFormat("HH:MM:SS");
    df.setLenient(false);
    try
    {
        Date d = df.parse(dthours);
        System.out.println("d = " + d);
    }
    catch (ParseException e)
    {
        System.out.println("parseError");
    }
}

The above snippet would print "parseError" for that input.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • @Vikdor -1 its a shame it produces a date which doesn't make any sense. ;) See my comment above. All this does is turn off the sanity check. – Peter Lawrey Aug 22 '12 at 11:57
  • @PeterLawrey, You mean to say "Thu Aug 01 16:52:43 IST 2374" when it is lenient? If so, true :) – Vikdor Aug 22 '12 at 11:58
  • The 4856 months and 35563333 millisecond sounds suspect to me. – Peter Lawrey Aug 22 '12 at 11:59
  • @PeterLawrey, a format is a format at the end of the day. If the user really wanted to parse the input in HH:MM:SS format, the API would just do that, right? Are you expecting the API to suggest HH:mm:ss? – Vikdor Aug 22 '12 at 12:02
  • 1
    @Keppil Thank you, I missed that point, changing to +1. I still suspect he wants "HH:mm:ss" rather than "HH:MM:SS" – Peter Lawrey Aug 22 '12 at 12:03
  • @PeterLawrey, frankly, I missed it initially and only realized after you edited your comment. – Vikdor Aug 22 '12 at 12:05
  • @Victor if i give String dthours="07:00:00"; also it is throwing a parse exception. – ashwinsakthi Aug 22 '12 at 12:09
  • @ashwinsakthi, because 00 is not a valid month. You should change the format to HH:mm:ss. You may want to see http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html for details on referring to different parts of a date. – Vikdor Aug 22 '12 at 12:10
  • 1
    @ashwinsakthi As I keep saying you want "HH:mm:ss" which means 24 hours, minutes and seconds. What you have is "HH:MM:SS" which means 24 hours, **months** and milli-seconds. There is no month `00` so this fails. – Peter Lawrey Aug 22 '12 at 12:11
  • But now for HH:mm:ss if i give date as String dthours="07:001:001".It is not throwing a parse error. – ashwinsakthi Aug 22 '12 at 12:11
  • Because those are still valid values for minute and second. – Vikdor Aug 22 '12 at 12:13
  • Thanks to both,but i need to make sure the date from front end is restricted to HH:mm:ss and not a single character should be extra in hours or minutes or seconds. – ashwinsakthi Aug 22 '12 at 12:14
  • Would you like to restrict through the regex (http://stackoverflow.com/questions/209192/matching-a-time-string-with-a-regular-expression)? – Vikdor Aug 22 '12 at 12:16
  • This link is pretty much what i wanted ..http://www.mkyong.com/regular-expressions/how-to-validate-time-in-24-hours-format-with-regular-expression/ – ashwinsakthi Aug 22 '12 at 12:33