LocalDateTime
@SpringBootTest
@Slf4j
public class DateTest {
@Test
public void localDateTime() {
LocalDateTime now = LocalDateTime.now();
LocalDateTime localDateTime1 = LocalDateTime.of(2017, 1, 14, 10, 34);
LocalDateTime localDateTime2 = LocalDateTime.of(2019, Month.MARCH, 28, 14, 33, 48, 0);
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter format2 = DateTimeFormatter.ofPattern("yyyy년 MM월 dd일 HH시 mm분 ss초");
String formatDateTime1 = now.format(format);
String formatDateTime2 = now.format(format2);
System.out.println(localDateTime1);
System.out.println(localDateTime2);
System.out.println(formatDateTime1);
System.out.println(formatDateTime2);
}
}
// 출력값
2017-01-14T10:34
2019-03-28T14:33:48
2022-05-31 13:00:18
2022년 05월 31일 13시 00분 18초
Period
두 날짜의 연, 월, 일 차이를 알고 싶을 때 사용하는 클래스
파라미터에는 LocalDate만 넣을 수 있습니다.
@SpringBootTest
@Slf4j
public class DateTest {
@Test
public void dateTest() {
LocalDate localDate1 = LocalDate.of(2017, Month.AUGUST, 1);
LocalDate localDate2 = LocalDate.of(2020, Month.FEBRUARY, 14);
Period between = Period.between(localDate1, localDate2);
Period until = localDate1.until(localDate2);
System.out.println("between: " + between);
System.out.println("until: " + until);
System.out.println("between.getYears(): " + between.getYears());
System.out.println("between.getMonths(): " + between.getMonths());
System.out.println("between.getDays(): " + between.getDays());
System.out.println("between.getChronology(): " + between.getChronology());
System.out.println("between.getUnits(): " + between.getUnits());
System.out.println("until.getYears(): " + until.getYears());
System.out.println("until.getMonths(): " + until.getMonths());
System.out.println("until.getDays(): " + until.getDays());
System.out.println("until.getChronology(): " + until.getChronology());
System.out.println("until.getUnits(): " + until.getUnits());
}
}
// 출력값
between: P2Y6M13D
until: P2Y6M13D
between.getYears(): 2
between.getMonths(): 6
between.getDays(): 13
between.getChronology(): ISO
between.getUnits(): [Years, Months, Days]
until.getYears(): 2
until.getMonths(): 6
until.getDays(): 13
until.getChronology(): ISO
until.getUnits(): [Years, Months, Days]
between 메서드나 until 메서드를 사용해서 얻은 Period 객체는 동일하므로 기억해 두는 것이 좋을 것 같습니다.
변수 localDate1의 날짜가 변수 localDate2 날짜보다 늦는다면 결과값은 음수로 바뀌어서 출력됩니다.
Duration
날짜가 아닌 시간의 차이를 알고 싶을 때 사용하는 클래스입니다.
@SpringBootTest
@Slf4j
public class DateTest {
@Test
public void durationTest() {
Instant instantNow = Instant.now();
Instant plus = instantNow.plus(10, ChronoUnit.SECONDS);
Duration between = Duration.between(instantNow, plus);
System.out.println(between.getSeconds());
}
}
// 출력값
10
날짜 간 차이 구하기
ChronoUnit Enum의 DAYS를 다른거로 바꾸면 연, 월, 주, 시간 등 단위로 바꿔서 계산할 수도 있습니다.
@SpringBootTest
@Slf4j
public class DateTest {
@Test
public void betweenTest() {
LocalDate localDate1 = LocalDate.of(2017, Month.AUGUST, 1);
LocalDate localDate2 = LocalDate.of(2020, Month.FEBRUARY, 14);
long between = ChronoUnit.DAYS.between(localDate1, localDate2);
System.out.println(between);
}
}
927