1

I want to parse search date from Angular. I make this query:

[HPM] GET /api/data/find?from=2019-07-24T00:00:00.000Z&page=0&size=10 

Using https://github.com/tkaczmarzyk/specification-arg-resolver framework I want to perform the search. I treid this configuration:

@Spec(path = "createdAt", params = "from", spec = GreaterThanOrEqual.class, config="yyyy-MM-dd'T'HH:mm:ss'Z'"),
                    @Spec(path = "createdAt", params = "to", spec = LessThanOrEqual.class, config="yyyy-MM-dd'T'HH:mm:ss'Z'")

But I get exception:

 java.time.format.DateTimeParseException: Text '2019-07-24T00:00:00.000Z' could not be parsed at index 19

Do you know what date format I need to set?

4
  • try yyyy-MM-dd'T'HH:mm:ss.SSS'Z' Jul 23, 2019 at 21:19
  • Yes, this the solution, thanks! Jul 23, 2019 at 21:24
  • No, it’s not. Z is an offset of 0 from UTC and needs to be parsed as such, or you will get an incorrect time (on most JVMs). The format is standard (ISO 8601), so may be built-in, otherwise try uuuu-MM-dd'T'HH:mm:ss.SSSX.
    – Anonymous
    Jul 24, 2019 at 10:02
  • uuuu-MM-dd'T'HH:mm:ss.SSSX this one is also working. So finally which one is recommended? Jul 24, 2019 at 15:50

1 Answer 1

0

TL;DR

uuuu-MM-dd'T'HH:mm:ss.SSSX

Discussion

There are two things wrong with the format pattern string you tried, yyyy-MM-dd'T'HH:mm:ss'Z'.

  1. It doesn’t include fraction of second. The string you are trying to parse, 2019-07-24T00:00:00.000Z, has three decimals on the seconds, so you need to specify three S in the format pattern string. The exception you got points to index 19, that is exactly where the decimal point is. Counting your way to the index given is often helpful.
  2. The trailing Z in your date-time string means UTC or offset 0 from UTC. You need to parse it as such or you will get an incorrect time (unless your JVM’s time zone happens to coincide with UTC, but this is both obscure and fragile, you will not want to rely on it).

Whether you use yyyy or uuuu for year probably doesn’t matter. I include a link below for a discussion of the difference.

There is a difference between using X, XX, XXX, XXXX and XXXXX, but they all match Z. So if your date-time string always comes with a Z, it doesn’t matter how many X you put. See the documentation link for details.

Another tip: When parsing using some format pattern string doesn’t work, try formatting a string using the pattern that you are trying and see how the formatted string differs from the one you were trying to parse. It will often give you a hint. For example:

    DateTimeFormatter testFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
    System.out.println(LocalDate.of(2019, Month.JULY, 24).atStartOfDay(ZoneId.systemDefault()).format(testFormatter));
    System.out.println("2019-07-24T00:00:00.000Z");

In the output the difference between the strings is clear enough:

2019-07-24T00:00:00Z
2019-07-24T00:00:00.000Z

(This doesn’t detect the incorrect use of the quoted 'Z', though.)

Links

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.