@JsonFormat DEFAULT_TIMEZONE doesn't seem to be working
Solved my own question. Here is what I found:
JsonFormat.DEFAULT_TIMEZONE
is NOT the system default, as the documentation and SO answer suggest, but actually defaults to UTC.
org.springframework.http.converter.json.Jackson2ObjectMapperBuilder
/**
* Override the default {@link TimeZone} to use for formatting.
* Default value used is UTC (NOT local timezone).
* @since 4.1.5
*/
public Jackson2ObjectMapperBuilder timeZone(TimeZone timeZone) {
com.fasterxml.jackson.annotation.JsonFormat
/**
* Value that indicates that default {@link java.util.TimeZone}
* (from deserialization or serialization context) should be used:
* annotation does not define value to use.
*<p>
* NOTE: default here does NOT mean JVM defaults but Jackson databindings
* default, usually UTC, but may be changed on <code>ObjectMapper</code>.
*/
public final static String DEFAULT_TIMEZONE = "##default";
Solution:
@Autowired
com.fasterxml.jackson.databind.ObjectMapper objectMapper;
and objectMapper.setTimeZone(TimeZone.getDefault())
in a config class, like so:
package path.to.config;
import ...
@Configuration
public class JacksonConfiguration {
@Autowired
public JacksonConfiguration(ObjectMapper objectMapper){
objectMapper.setTimeZone(TimeZone.getDefault());
}
}
This should set the Jackson ObjectMapper to use system default instead of Jackson default (UTC).
Jackson what to set the timezone for JSON parsing?
Date is probably not a good object since you have to parse HH:mm.
Dates represent a certain instance in time after 1/1/1970 UTC. And you can't represent that with only an hour from 0-23
You should probably look into making a custom HourMinute object and a custom Deserializer/Serializer
You would also take timezone into consideration as some sort of offset.
What if i'm in berlin and i set my earliest pickup time to 1.00
Is the time -5am in new york?
Jackson @JsonFormat set date with one day less
Use this solution, it is more effective and modern than my solution: https://stackoverflow.com/a/45456037/4886918
Thanks @Benjamin Lucidarme.
I resolved my problem using:
@Temporal(TemporalType.DATE)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy", locale = "pt-BR", timezone = "Brazil/East")
private Date birthDate;
I changed timezone to "Brazil/East" or "America/Sao_Paulo" and working now
Thanks
Jackson jsonformat deserialize always in UTC
timezone
parameter is meant for serialisation according to JsonFormat documentation and not for deserialisation as in your post. I don't see how it could be used for deserialisation given that Date "is intended to reflect coordinated universal time (UTC)" and doesn't contain timezone information.
Consider switching to ZonedDateTime, available since Java 8, if you want to specify a timezone for deliveryDate
.
If you must use Date
you can specify a default timezone that will be used whenever you print or format any Date
object E.g.
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Singapore"));
System.out.println(new Date());
produces
Tue Mar 06 21:15:12 SRET 2018
Jackson @JsonFormat converting date with incorrect timezone
Simple solution: I solved it by changing the data type to String
which completes my aim to capture the value as it is coming from JSON payload. Using Date
and other data types were converting the value to some different timezone.
@JsonProperty("callStartTime")
@Column(name = "call_start_dt", nullable = false)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", lenient = OptBoolean.FALSE)
private **String** callStartTime;
Related Topics
Spring Rest Post Json Requestbody Content Type Not Supported
How to Get Http Response Code Using Selenium Webdriver
How to Send List of Long to Rest Controller Spring Boot
Java 8 Streams - How to Merge Elements from the List With the Same Fields to One Element and Sum Up
Showing Morning, Afternoon, Evening, Night Message Based on Time in Java
Set Layout Width Percentage of the Total Screen Width
A Jsonobject Text Must Begin With '{' At 1 [Character 2 Line 1] With '{' Error
Creating a Composite Unique Constraints on Multiple Columns
Transactionrequiredexception Executing an Update/Delete Query
Flush/Clear System.In (Stdin) Before Reading
Gradle: How to Exclude a Particular Package from a Jar
How to Get Numbers from Given Gcd and Lcm
Spring Boot JPA Unknown Column in Field List
Junit 5 - No Parameterresolver Registered for Parameter
Check If One List Contains Element from the Other