In REST API Response I am getting date as '2016-07-02T00:00:00Z'
In code, I have a column like below
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss", timezone = "UTC")
private Instant effectiveDate;
// getter & setter
public Instant getEffectiveDate() {
return effectiveDate;
}
public OfferTerm setEffectiveDate(Instant effectiveDate) {
this.effectiveDate = effectiveDate;
return this;
}
Getting below error:
java.time.format.DateTimeParseException: Text '2016-07-02T00:00:00Z' could not be parsed, unparsed text found at index 19
Please help me here.
Your date-time string is already in the format that
Instant#parse
uses by default. Given below is a demo to illustrate the same:Output:
Since an
Instant
represents an instantaneous point on the UTC time-line (i.e. it is independent oftimezone
), mentioningtimezone = "UTC"
is unnecessary. TheZ
in your date-time string is already specifying UTC. Moreover, the pattern,"yyyy-MM-dd'T'HH:mm:ss"
does not match the pattern of your date-time string. I hope, you understand that using thetimezone
andpattern
forInstant
is not only unnecessary but also the root cause of the error you have faced.Simply remove
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss", timezone = "UTC")
to resolve the issue.