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.