자바로 구성된 애플리케이션에서 자주 발생하는 문제중 LocalDateTime 과 OffsetDateTime 간의 컨버팅이 필요할 때가 있다.
OffsetDateTime ➡️ LocalDateTime
@Test
public void offsetToLocalTest() {
DateTimeFormatter formatter1 = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
DateTimeFormatter formatter2 = DateTimeFormatter.ISO_DATE_TIME;
TemporalAccessor input = formatter1.parse("2023-03-13T01:00:00+01:00");
OffsetDateTime offset = OffsetDateTime.from(input);
LocalDateTime local = offset
.atZoneSameInstant(ZoneId.systemDefault())
.toLocalDateTime();
System.out.println("Offset: " + formatter2.format(offset));
System.out.println("Local : " + formatter2.format(local));
Date offDate = Date.from(offset.toInstant());
Date localDate = Date.from(local.atZone(ZoneId.systemDefault()).toInstant());
Assertions.assertThat(offDate).isEqualTo(localDate);
}
LocalDateTime ➡️ OffsetDateTime
@Test
public void localToOffsetTest() {
DateTimeFormatter formatter1 = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
DateTimeFormatter formatter2 = DateTimeFormatter.ISO_DATE_TIME;
TemporalAccessor input = formatter2.parse("2023-03-13T09:00:00");
LocalDateTime local = LocalDateTime.from(input);
OffsetDateTime offset = local.atZone(ZoneId.systemDefault()).toOffsetDateTime();
offset = offset.withOffsetSameInstant(ZoneOffset.UTC);
System.out.println("Local : " + formatter2.format(local));
System.out.println("Offset: " + formatter2.format(offset));
Date offDate = Date.from(offset.toInstant());
Date localDate = Date.from(local.atZone(ZoneId.systemDefault()).toInstant());
Assertions.assertThat(offDate).isEqualTo(localDate);
}
전체 소스 코드
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
public class DateTimeTest {
@Test
public void offsetToLocalTest() {
DateTimeFormatter formatter1 = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
DateTimeFormatter formatter2 = DateTimeFormatter.ISO_DATE_TIME;
TemporalAccessor input = formatter1.parse("2023-03-13T01:00:00+01:00");
OffsetDateTime offset = OffsetDateTime.from(input);
LocalDateTime local = offset
.atZoneSameInstant(ZoneId.systemDefault())
.toLocalDateTime();
System.out.println("Offset: " + formatter2.format(offset));
System.out.println("Local : " + formatter2.format(local));
Date offDate = Date.from(offset.toInstant());
Date localDate = Date.from(local.atZone(ZoneId.systemDefault()).toInstant());
Assertions.assertThat(offDate).isEqualTo(localDate);
}
@Test
public void localToOffsetTest() {
DateTimeFormatter formatter1 = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
DateTimeFormatter formatter2 = DateTimeFormatter.ISO_DATE_TIME;
TemporalAccessor input = formatter2.parse("2023-03-13T09:00:00");
LocalDateTime local = LocalDateTime.from(input);
OffsetDateTime offset = local.atZone(ZoneId.systemDefault()).toOffsetDateTime();
offset = offset.withOffsetSameInstant(ZoneOffset.UTC);
System.out.println("Local : " + formatter2.format(local));
System.out.println("Offset: " + formatter2.format(offset));
Date offDate = Date.from(offset.toInstant());
Date localDate = Date.from(local.atZone(ZoneId.systemDefault()).toInstant());
Assertions.assertThat(offDate).isEqualTo(localDate);
}
}
'개발자의 정보 > Java & framework' 카테고리의 다른 글
SpringBoot Security multiple PasswordEncoder 설정 (0) | 2023.04.04 |
---|---|
Spring batch with multiple db (0) | 2023.03.24 |
spring @Conditional 사용하기 (0) | 2023.03.12 |
spring6 3가지 rest-client 사용하기 (0) | 2023.03.11 |
Note: Recompile with -Xlint:unchecked for details. (0) | 2023.03.10 |
java.lang.ClassNotFoundException: javax.xml.bind.DatatypeConverter (0) | 2023.03.01 |
spring rest 서비스중 error 응답에서 trace 제거하기 (0) | 2023.02.28 |
spring-framework 에서 error 응답 json 으로보내기 (0) | 2023.02.28 |
댓글