본문 바로가기
카테고리 없음

[java] LocalDate , LocalDateTime, LocalTime 차이

by 개발LOG 2024. 2. 12.

LocalDate :날짜

LocalDate today = LocalDate.now();
System.out.println("Today is " + today);

LocalDate birthday = LocalDate.of(1982, 7, 13);
System.out.println("My birthday is " + birthday);

LocalDate christmas = LocalDate.parse("2017-12-25");
System.out.println("Last Christmas is " + christmas);

결과:

Today is 2018-06-17
My birthday is 1982-07-13
Last Christmas is 2017-12-25

위 예제 코드 처럼 LocalDate 클래스는 public 생성자를 제공하지 않기 때문에 객체를 생성할 때는 now()나, of(), parse()와 같은 정적 메소드를 사용하도록 되어 있습니다. 기본 포맷인 yyyy-MM-dd 형태의 문자열을 parse() 메소드에 넘길 수 있습니다.

 

LocalTime: HH:mm:ss.SSS

LocalTime currentTime = LocalTime.now();
System.out.println("The current time here is " + currentTime);

LocalTime currentTimeInParis = LocalTime.now(ZoneId.of("Europe/Paris"));
System.out.println("The current time in Paris is " + currentTimeInParis);

LocalTime timeToGoToBed = LocalTime.of(23, 30, 0);
System.out.println("I go to bed at " + timeToGoToBed);

LocalTime timeToGetUp = timeToGoToBed.plusHours(8);
System.out.println("I get up at " + timeToGetUp);

System.out.println("I still go to bed at " + timeToGoToBed);

결과:

The current time here is 11:57:33.804
The current time in Paris is 04:57:33.806
I go to bed at 23:30
I get up at 07:30
I still go to bed at 23:30

 

LocalDateTime: yyyy-MM-dd'T'HH-mm-ss.SSS

LocalDateTime now = LocalDateTime.now();
System.out.println("Now is " + now);

LocalDateTime now2 = LocalDateTime.of(LocalDate.now(), LocalTime.now());
System.out.println("Now is " + now2);

LocalDateTime y2k = LocalDateTime.parse("1999-12-31T23:59:59.999");
System.out.println("Y2K is " + y2k);

LocalDateTime dateOfBirth = LocalDateTime.of(1982, 7, 13, 14, 25, 00);
System.out.println("My date of birth is " + dateOfBirth);

LocalDateTime dateOfBirth2 = Year.of(1982).atMonth(7).atDay(13).atTime(14, 25);
System.out.println("My date of birth is " + dateOfBirth2);

결과:

Now is 2018-06-17T22:46:17.348
Now is 2018-06-17T22:46:17.408
Y2K is 1999-12-31T23:59:59.999
My date of birth is 1982-07-13T14:25
My date of birth is 1982-07-13T14:25

 

 

유틸리티 메서드 활용

LocalDate today = Year.of(2017).atMonth(12).atDay(17);
System.out.println("Today : " + today);
System.out.println("Year : " + today.getYear());
System.out.println("Month : " + today.getMonth());
System.out.println("DayOfMonth : " + today.getDayOfMonth());
System.out.println("DayOfWeek : " + today.getDayOfWeek());
System.out.println("IsLeapYear : " + today.isLeapYear());

System.out.println("1 year after today: " + today.plusYears(1));
System.out.println("1 month after today: " + today.plusMonths(1));
System.out.println("1 day after today: " + today.plusDays(1));

LocalDate yesterday = today.minusDays(1);
System.out.println("Today is after yesterday? " + today.isAfter(yesterday));
System.out.println("Today is before yesterday? " + today.isBefore(yesterday));

LocalDateTime now = today.atTime(22, 46, 17, 348);
System.out.println("Now : " + now);
System.out.println("Hour: " + now.getHour());
System.out.println("Minute: " + now.getMinute());
System.out.println("Second: " + now.getSecond());
System.out.println("Nano: " + now.getNano());
Today : 2017-12-17
Year : 2017
Month : DECEMBER
DayOfMonth : 17
DayOfWeek : SUNDAY
IsLeapYear : false
1 year after today: 2018-12-17
1 month after today: 2018-01-17
1 day after today: 2017-12-18
Today is after yesterday? true
Today is before yesterday? false
Now : 2017-12-17T22:46:17.000000348
Hour: 22
Minute: 46
Second: 17
Nano: 348