0

I got function:

    public boolean isValidDate(String inDate) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:sssZ");
        dateFormat.setLenient(false);
    try {
      dateFormat.parse(inDate.trim());
    } catch (ParseException pe) {
      return false;
    }
    return true;
  }

And I need to verify if string: '2015-01-06T10:51:26.227+0100' is date, but funtion always return false. Could anybody tell me if my dateDormat: "yyyy-MM-dd'T'HH:mm:sssZ" is correct? I read a lot of questions about formatting date, but I think my dateFormat is still not correct.

  • 1
    Do you know such thing as DEBUG? – Andremoniy Aug 19 '16 at 10:55
  • Have the formatter format the actual date, then look at the output and see if it matches what you are trying to parse. But I guess the `:sss` of the format is not correct. It should be `yyyy-MM-dd'T'HH:mm:ss.SSSZ` – Fildor Aug 19 '16 at 10:55
  • Which version of Java? – fge Aug 19 '16 at 10:57
  • Ok, I wrote something like this to get test if its correct: `SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); Date date = new Date(); String str = dateFormat.format(date);` and on output I got: a: 2016-08-19T13:05:022+0200 so it's not generating miliseconds correctly – Czeslaw Czeslaw Aug 19 '16 at 11:04

3 Answers3

4

Errors

  • missing milliseconds S
  • missing dot . between seconds and milliseconds
  • putting 3 seconds s

Your format should be

yyyy-MM-dd'T'HH:mm:ss.SSSZ
Fildor
  • 14,510
  • 4
  • 35
  • 67
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
3

I guess the :sss part of the format is not correct. That would be 3 seconds, no Millis and a missing dot.

It should be :

yyyy-MM-dd'T'HH:mm:ss.SSSZ

To debug this: Have the formatter format the current date, then look at the output and see if it matches what you are trying to parse.

The above pattern is actually taken from SimpleDateFormat - Examples Section.

In Java 8 you should use the new Date/Time classes like described in this answer.

Community
  • 1
  • 1
Fildor
  • 14,510
  • 4
  • 35
  • 67
1

tl;dr

No formatting pattern necessary.

java.util.Date utilDate = java.util.Date.from( OffsetDateTime.parse( "2015-01-06T10:51:26.227+0100" ).toInstant() );

For invalid inputs, catch DateTimeParseException.

java.time

Other Answers are correct but use outdated classes. The troublesome old legacy date-time classes have been supplanted by java.time classes.

Your input string is in standard ISO 8601 format. The java.time uses ISO 8601 formats by default for parsing and generating strings. So need to specify a parsing pattern.

Your input has an offset-from-UTC at the end. So parse as an OffsetDateTime.

OffsetDateTime odt = OffsetDateTime.parse( "2015-01-06T10:51:26.227+0100" );

Catching a DateTimeParseException will indicate the input data is invalid.

If you must have a java.util.Date object for use with old code not yet updated for java.time types, you can convert. Look for new methods added to the old classes for convenient conversions.

java.util.Date utilDate = java.util.Date.from( odt.toInstant() ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154