feat: LocalDate 처리 적용
parent
6b15987f3f
commit
048d6b9d24
@ -0,0 +1,107 @@
|
||||
package com.xit.core.config.support;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.databind.Module;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
/**
|
||||
* DTO LocalDate 필드의 아래의 선언 처리
|
||||
* @JsonFormat(pattern="yyyy-MM-dd")
|
||||
* @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
|
||||
*
|
||||
* Request(클라이언트 -> 서버)로 전달할 때는 @DateTimeFormat
|
||||
* Responst (서버 -> 클라이언트) 로 전달할 때에는 @JsonFormat
|
||||
*
|
||||
*/
|
||||
//@Configuration
|
||||
@Slf4j
|
||||
public class LocalDateTimeConfiguration {
|
||||
private static final String DEFAULT_DATE_FMT = "yyyy-MM-dd";
|
||||
private static final String DEFAULT_DATE_TIME_FMT = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
@Bean
|
||||
@Primary
|
||||
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
|
||||
log.info(">>>>>>>>>>> Config is starting.");
|
||||
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
|
||||
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
|
||||
return objectMapper;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Module jsonMapperJava8DateTimeModule() {
|
||||
SimpleModule module = new SimpleModule();
|
||||
|
||||
module.addDeserializer(ZonedDateTime.class, new JsonDeserializer<ZonedDateTime>() {
|
||||
@Override
|
||||
public ZonedDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
log.info(">>>>>>>>>>>>>>>>>>>>>LocalDateTimeConfiguration::addDeserializer-ZonedDateTime");
|
||||
return ZonedDateTime.parse(p.getValueAsString(), DateTimeFormatter.ISO_ZONED_DATE_TIME);
|
||||
}
|
||||
});
|
||||
|
||||
module.addDeserializer(LocalDateTime.class, new JsonDeserializer<LocalDateTime>() {
|
||||
@Override
|
||||
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
log.info(">>>>>>>>>>>>>>>>>>>>>LocalDateTimeConfiguration::addDeserializer-LocalDateTime");
|
||||
return LocalDateTime.parse(p.getValueAsString(), DateTimeFormatter.ISO_LOCAL_DATE_TIME);
|
||||
//return LocalDateTime.parse(p.getValueAsString(), DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FMT));
|
||||
}
|
||||
});
|
||||
|
||||
module.addDeserializer(LocalDate.class, new JsonDeserializer<LocalDate>() {
|
||||
@Override
|
||||
public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
|
||||
log.info(">>>>>>>>>>>>>>>>>>>>>LocalDateTimeConfiguration::addDeserializer-LocalDate");
|
||||
//return LocalDateTime.parse(p.getValueAsString(), DateTimeFormatter.ISO_DATE_TIME);
|
||||
return LocalDate.parse(p.getValueAsString(), DateTimeFormatter.ofPattern(DEFAULT_DATE_FMT));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
module.addSerializer(ZonedDateTime.class, new JsonSerializer<ZonedDateTime>() {
|
||||
@Override
|
||||
public void serialize(ZonedDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||
log.info(">>>>>>>>>>>>>>>>>>>>>LocalDateTimeConfiguration::addSerializer-ZonedDateTime");
|
||||
gen.writeString(DateTimeFormatter.ISO_ZONED_DATE_TIME.format(value));
|
||||
}
|
||||
});
|
||||
|
||||
module.addSerializer(LocalDateTime.class, new JsonSerializer<LocalDateTime>() {
|
||||
@Override
|
||||
public void serialize(LocalDateTime localDateTime, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||
log.info(">>>>>>>>>>>>>>>>>>>>>LocalDateTimeConfiguration::addSerializer-LocalDateTime");
|
||||
gen.writeString(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(localDateTime));
|
||||
//gen.writeString(localDateTime.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FMT)));
|
||||
}
|
||||
});
|
||||
|
||||
module.addSerializer(LocalDate.class, new JsonSerializer<LocalDate>() {
|
||||
@Override
|
||||
public void serialize(LocalDate localDate, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||
log.info(">>>>>>>>>>>>>>>>>>>>>LocalDateTimeConfiguration::addSerializer-LocalDate");
|
||||
//gen.writeString(DateTimeFormatter.ISO_DATE_TIME.format(value));
|
||||
gen.writeString(localDate.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_FMT)));
|
||||
}
|
||||
});
|
||||
|
||||
return module;
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package com.xit.core.util.security;
|
||||
|
||||
import com.xit.core.util.Checks;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.sql.Date;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class ConvertUtils {
|
||||
private static final String DEFAULT_DATE_FMT = "yyyy-MM-dd";
|
||||
private static final String DEFAULT_DATE_TIME_FMT = "yyyy-MM-dd HH:mm:ss";
|
||||
|
||||
public static String getStringDateFromDate(@NotNull Date date, @Nullable String format) {
|
||||
if(Checks.isNull(format)) format = "DEFAULT_DATE_FMT";
|
||||
return new SimpleDateFormat(format).format(date);
|
||||
}
|
||||
|
||||
public static String getStringDateTimeFromDate(@NotNull Date date, @Nullable String format) {
|
||||
if(Checks.isNull(format)) format = "DEFAULT_DATE_TIME_FMT";
|
||||
return new SimpleDateFormat(format).format(date);
|
||||
}
|
||||
|
||||
public String getStringDateFromLocalDate(@NotNull LocalDate localDate, @Nullable String format) {
|
||||
if(Checks.isNull(format)) format = "DEFAULT_DATE_FMT";
|
||||
return localDate.format(DateTimeFormatter.ofPattern(format));
|
||||
}
|
||||
|
||||
public String getStringDateTimeFromLocalDate(@NotNull LocalDate localDate, @Nullable String format) {
|
||||
if(Checks.isNull(format)) format = "DEFAULT_DATE_TIME_FMT";
|
||||
return localDate.format(DateTimeFormatter.ofPattern(format));
|
||||
}
|
||||
|
||||
public static java.util.Date toDateFromLocalDate(LocalDate localDate){
|
||||
Instant instant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant();
|
||||
return java.util.Date.from(instant);
|
||||
}
|
||||
|
||||
public static java.util.Date toDateFromLocalDateTime(LocalDateTime localDateTime){
|
||||
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
|
||||
return java.util.Date.from(instant);
|
||||
}
|
||||
|
||||
public static LocalDate toLocalDateFromDate(java.util.Date date){
|
||||
return LocalDate.ofInstant(date.toInstant(), ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
public static LocalDateTime toLocalDateTimeFromDate(java.util.Date date){
|
||||
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue