티스토리 뷰
LocalDateTime의 Format
1. DateTimeFormatter.ISO_DATE_TIME
- 2018-10-10T00:00:00.000+09:00
- 2018-10-10T00:00:00.000Z
2. 그밖의 format
- yyyyMMddHHmmss
- yyyy-MM-dd HH:mm:ss
- yyyy-MM-dd
LocalDateTime의 다양한 변환
1. LocalDateTime <-> sql.TimeStamp
import java.sql.Timestamp;
@Converter(autoApply = true)
public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) {
return (localDateTime == null) ? null : Timestamp.valueOf(localDateTime);
}
@Override
public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
return (sqlTimestamp == null) ? null : sqlTimestamp.toLocalDateTime();
}
}
2. LocalDateTime <-> sql.Date
import java.sql.Date;
@Converter(autoApply = true)
public class LocalDateConverter implements AttributeConverter<LocalDate, Date> {
@Override
public Date convertToDatabaseColumn(LocalDate localDate) {
return (localDate == null) ? null : Date.valueOf(localDate);
}
@Override
public LocalDate convertToEntityAttribute(Date sqlDate) {
return (sqlDate == null) ? null : sqlDate.toLocalDate();
}
}
3. LocalDateTime <-> json
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
public final class LocalDateTimeJsonSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
jgen.writeString(DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(value));
}
}
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
public final class LocalDateTimeJsonDeserializer extends JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
return LocalDateTime.parse(p.getText(), DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
}
}
4. LocalDateTime <-> String
public LocalDateTime toLocalDateTime(DateTimeFormatter formatter, String localDateTime) {
return LocalDateTime.parse(localDateTime, formatter);
}
public String toString(DateTimeFormatter formatter, LocalDateTime localDateTime) {
return localDateTime.format(formatter);
}
5. LocalDateTime <-> millisecond
public LocalDateTime toLocalDateTime(Long millisecond) {
return Instant.ofEpochMilli(millisecond).atZone(ZoneId.systemDefault()).toLocalDateTime();
}
public Long toEpochMillis(LocalDateTime localDateTime) {
return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
'Programming > Java' 카테고리의 다른 글
상속과 this (0) | 2020.01.12 |
---|---|
Reflection (0) | 2019.06.18 |
JMX - mBean (2) | 2018.11.15 |
StringBuffer, StringBuilder (0) | 2016.08.23 |
자료구조 (0) | 2016.08.22 |
- Total
- Today
- Yesterday
- JPA Criteria
- Property
- Spring
- Spring JDBC Template
- 복합키 Mapping
- Query DSL
- Embeddable Mapping
- java Equals
- Criteria
- RetryTemplate
- java EqualsAndHashCode
- @Primary
- SmartLifecycle
- Join Table
- JPA
- Sprint RetryTemplate
- Spring Registrar
- DI
- Embedded Mapping
- java generic
- scikit-learn
- Mapping
- Typesafe Config
- spring spel
- Charles proxy
- Registrar
- docker
- guava
- Akka
- Discriminate Mapping
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |