0

I have a JSON file with a value of 2020-03-07T04:11:20.000 for a field with given pattern yyyy-MM-dd'T'HH:mm:ss.SSS. I tried to add a Z at the end of it, but it keeps failing the validation. Any idea why?

I tried to do OffsetDateTime.parse(mytext, DateTimeFormatter.ofPattern(mypattern)) and it threw DateTimeParseException with Unable to obtain OffsetDateTime from TemporalAccessor

user998692
  • 5,172
  • 7
  • 40
  • 63
  • 1
    What is the exception being thrown, if any? Show the code that is producing this. – Jason Aug 20 '20 at 12:42
  • A date-time field? LocalDateTime? (Not LocalDate) – Joop Eggen Aug 20 '20 at 12:44
  • Well, what does the validation do? – deHaar Aug 20 '20 at 12:45
  • What code are you using to validate? – Joni Aug 20 '20 at 12:45
  • I tried to do OffsetDateTime.parse(mytext, DateTimeFormatter.ofPattern(mypattern)) and it threw DateTimeParseException with Unable to obtain OffsetDateTime from TemporalAccessor – user998692 Aug 20 '20 at 12:45
  • The field in the json has type OffsetDateTime and is annotated with that pattern. – user998692 Aug 20 '20 at 12:46
  • There is no offset in the `String`, you will have to add one when parsing... – deHaar Aug 20 '20 at 12:47
  • What do you mean by offset? Could you please give me an example of amended string? – user998692 Aug 20 '20 at 12:49
  • I mean something inside the `String` that tells how many hours the value differs from UTC. For example: `2020-03-07T04:11:20.000+01:00` – deHaar Aug 20 '20 at 12:51
  • The JSON annotation is @JsonFormat(shape=Shape.String, pattern = my pattern, timezone="UTC"). – user998692 Aug 20 '20 at 12:55
  • @user998692 Thank you for providing a wealth of good information in response comments. It’s even better to [edit](https://stackoverflow.com/posts/63505330/edit) the question and add all the information there so we have everything in one place. – Ole V.V. Aug 21 '20 at 18:13

2 Answers2

4

Since neither the pattern nor the input has a timezone offset, you can't parse it directly to OffsetDateTime. What you can do is parse the date as LocalDateTime and add the offset to the result.

For example, using ZoneOffset.UTC:

LocalDateTime ldt = LocalDateTime.parse(mytext, DateTimeFormatter.ofPattern(mypattern));
OffsetDateTime odt = ldt.atOffset(ZoneOffset.UTC)
Joni
  • 108,737
  • 14
  • 143
  • 193
2

First of all, you are using the wrong class, OffsetDateTime for your case. Since you have mentioned timezone="UTC", you should use ZonedDateTime. Note that after using the following annotation, the date-time produced will be like 2020-03-07T04:11:20.000 UTC.

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS z", timezone="UTC")

which you can parse into ZonedDateTime using the pattern, yyyy-MM-dd'T'HH:mm:ss.SSS z.

Demo:

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        String myPattern = "yyyy-MM-dd'T'HH:mm:ss.SSS z";
        String myText = "2020-03-07T04:11:20.000 UTC";
        System.out.println(ZonedDateTime.parse(myText, DateTimeFormatter.ofPattern(myPattern)));
    }
}

Output:

2020-03-07T04:11:20Z[UTC]

If you want to keep the date-time format as 2020-03-07T04:11:20.000 then you should remove timezone="UTC" from the annotation and parse the obtained date-time string into a LocalDateTime instead of ZonedDateTime. Needless to say that the pattern should be yyyy-MM-dd'T'HH:mm:ss.SSS in that case.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110