Skip to content

Instantly share code, notes, and snippets.

@mzakyalvan
Last active August 7, 2018 09:33
Show Gist options
  • Save mzakyalvan/3f11ab7463201ef1b7297db9464755c0 to your computer and use it in GitHub Desktop.
Save mzakyalvan/3f11ab7463201ef1b7297db9464755c0 to your computer and use it in GitHub Desktop.
package sample;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
/**
* To simplify object mapper instantiation, run this with spring-boot-starter-web as dependency :D.
*
* If you use Spring Boot 1.5.x, don't forget to add dependencies from https://github.com/FasterXML/jackson-modules-java8
*
* Sample output :
*
* {"timezone":"GMT","timestamp":"2018-08-07 09:32:08 GMT"}
* {"timezone":"GMT+8","timestamp":"2018-08-07 17:32:08 GMT+08:00"}
* {"utcTime":"2018-08-07 09:32:08 UTC","plusFive":"2018-08-07 14:32:08 GMT+05:00","plusSix":"2018-08-07 15:32:08 GMT+06:00","plusSeven":"2018-08-07 16:32:08 GMT+07:00"}
*/
@SpringBootApplication
public class DateTimeDeserializeSample {
public static void main(String[] args) {
SpringApplication.run(DateTimeDeserializeSample.class);
}
@Component
static class DeserializeRunner implements ApplicationRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(DeserializeRunner.class);
@Autowired
private ObjectMapper objectMapper;
@Override
public void run(ApplicationArguments args) throws Exception {
ZonedDateTime dateTime = ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("GMT+7"));
LOGGER.info(objectMapper.writeValueAsString(new WithGmtPlusZeroDateTime(dateTime)));
LOGGER.info(objectMapper.writeValueAsString(new WithGmtPlusEightDateTime(dateTime)));
LOGGER.info(objectMapper.writeValueAsString(new DeserializeLegacyDate(new Date())));
}
}
public static class WithGmtPlusZeroDateTime {
private final String timezone = "GMT";
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss z", timezone = "GMT")
private final ZonedDateTime timestamp;
public WithGmtPlusZeroDateTime(ZonedDateTime timestamp) {
this.timestamp = timestamp;
}
public String getTimezone() {
return timezone;
}
public ZonedDateTime getTimestamp() {
return timestamp;
}
}
public static class WithGmtPlusEightDateTime {
private final String timezone = "GMT+8";
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss z", timezone = "GMT+8")
private final ZonedDateTime timestamp;
public WithGmtPlusEightDateTime(ZonedDateTime timestamp) {
this.timestamp = timestamp;
}
public String getTimezone() {
return timezone;
}
public ZonedDateTime getTimestamp() {
return timestamp;
}
}
/**
* By default, java.util.Date deserialized in utc time.
*/
public static class DeserializeLegacyDate {
private final Date timestamp;
public DeserializeLegacyDate(Date timestamp) {
this.timestamp = timestamp;
}
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss z")
public Date getUtcTime() {
return timestamp;
}
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss z", timezone = "GMT+5")
public Date getPlusFive() {
return timestamp;
}
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss z", timezone = "GMT+6")
public Date getPlusSix() {
return timestamp;
}
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss z", timezone = "GMT+7")
public Date getPlusSeven() {
return timestamp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment