import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
public class LocalDateTimeEx {
public static void main(String[] args) {
// LocalDateTime
// : ๋ ์ง์ ์๊ฐ์ ๋ค๋ฃจ๋ ํด๋์ค๋ก ํ์์กด์ ํฌํจํ์ง ์๊ณ ์์คํ
์ ๋ ์ง/์๊ฐ์ ํํํฉ๋๋ค.
// 2024๋
8์ 23์ผ 10์ 0๋ถ 0์ด
LocalDateTime todayDateTime = LocalDateTime.of(2024, 8, 23, 10, 0, 0);
System.out.println("์ค๋ : " + todayDateTime);
// 2025๋
2์ 10์ผ 18์ 20๋ถ 0์ด
LocalDateTime endDateTime = LocalDateTime.of(2025, 2, 10, 18, 20, 0);
System.out.println("์ข
๊ฐ : " + endDateTime);
// A.isBefore(B) : A๊ฐ B๋ณด๋ค ์ด์ ์ด๋ฉด, true ์๋๋ฉด false
// A.isEqual(B) : A๊ฐ B๋ณด๋ค ๊ฐ์ผ๋ฉด, true ์๋๋ฉด false
// A.isAfter(B) : A๊ฐ B๋ณด๋ค ์ดํ์ด๋ฉด, true ์๋๋ฉด false
if( todayDateTime.isBefore(endDateTime) ) {
System.out.println("๊ต์ก๊ณผ์ ์งํ ์ค์
๋๋ค.");
}
else if( todayDateTime.isAfter(endDateTime) ) {
System.out.println("๊ต์ก๊ณผ์ ์ด ์ข
๋ฃ๋์์ต๋๋ค.");
}
else {
System.out.println("์ค๋์ ์ฆ๊ฑฐ์ด ์ข
๊ฐ๋ ์
๋๋ค.");
}
// until()
// : LocalDateTime ๊ฐ์ฒด์ ๋ ์ง/์๊ฐ๋ถํฐ ์ธ์๋ก ์ง์ ํ ๊ฐ์ฒด์ ๋ ์ง์๊ฐ๊น์ง
// ๋จ์ ์๊ฐ์ ๋ฐํํ๋ ๋ฉ์๋
// * ChronoUnit : ๋ ์ง/์๊ฐ์ ๋จ์๋ฅผ ๋ํ๋ด๋ ์ด๊ฑฐํ์
(enum)
// ๋ ์ง/์๊ฐ ์ฐจ์ด๋ฅผ ๊ตฌํ๊ฑฐ๋ ์กฐ์ํ๋ ๋ฐ์ ์ฌ์ฉ
long remainYear = todayDateTime.until(endDateTime, ChronoUnit.YEARS);
long remainMonth = todayDateTime.until(endDateTime, ChronoUnit.MONTHS);
long remainDay = todayDateTime.until(endDateTime, ChronoUnit.DAYS);
long remainHour = todayDateTime.until(endDateTime, ChronoUnit.HOURS);
long remainMinute = todayDateTime.until(endDateTime, ChronoUnit.MINUTES);
long remainSecond = todayDateTime.until(endDateTime, ChronoUnit.SECONDS);
System.out.println("*์ค๋๋ถํฐ ์ข
๊ฐ๊น์ง");
System.out.println("๋จ์ ์ฐ๋ : " + remainYear);
System.out.println("๋จ์ ๊ฐ์ ์ : " + remainMonth);
System.out.println("๋จ์ ์ผ ์ : " + remainDay);
System.out.println("๋จ์ ์ : " + remainHour);
System.out.println("๋จ์ ๋ถ : " + remainMinute);
System.out.println("๋จ์ ์ด : " + remainSecond);
// between()
// - ChronoUnit.์์.between(์์์ผ์, ์ข
๋ฃ์ผ์)
// : ์์์ผ์์ ์ข
๋ฃ์ผ์์ ๋จ์ ์๊ฐ์ ์ง์ ํ ์์ ๋จ์์ ๋ง๊ฒ ๋ณํํ๋ ๋ฉ์๋
remainYear = ChronoUnit.YEARS.between(todayDateTime, endDateTime);
remainMonth = ChronoUnit.MONTHS.between(todayDateTime, endDateTime);
remainDay = ChronoUnit.DAYS.between(todayDateTime, endDateTime);
remainHour = ChronoUnit.HOURS.between(todayDateTime, endDateTime);
remainMinute = ChronoUnit.MINUTES.between(todayDateTime, endDateTime);
remainSecond = ChronoUnit.SECONDS.between(todayDateTime, endDateTime);
System.out.println("*์ค๋๋ถํฐ ์ข
๊ฐ๊น์ง (ChronoUnit - between)");
System.out.println("๋จ์ ๋
๋ : " + remainYear);
System.out.println("๋จ์ ๊ฐ์ ์ : " + remainMonth);
System.out.println("๋จ์ ์ผ ์ : " + remainDay);
System.out.println("๋จ์ ์ : " + remainHour);
System.out.println("๋จ์ ๋ถ : " + remainMinute);
System.out.println("๋จ์ ์ด : " + remainSecond);
System.out.println();
// ๋ ์ง/์๊ฐ์ ์กฐ์
LocalDateTime now = LocalDateTime.now();
System.out.println("ํ์ฌ ๋ ์ง/์๊ฐ : " + now);
LocalDateTime targetTime = now.plusYears(1)
.plusMonths(5)
.plusDays(20)
.plusHours(5)
.plusMinutes(30)
.plusSeconds(15)
.minusYears(5)
// minusXXX()
;
// ๋น๋ ํจํด, (๋ฉ์๋ ์ฒด์ด๋)
System.out.println("๋ณ๊ฒฝ ๋ ์ง/์๊ฐ : " + targetTime);
}
}
Java
๋ณต์ฌ