programming/JAVA

[Java] LocalDate와 LocalDateTime 사용하기

LeeBorn 2020. 10. 25. 15:32
반응형

Java

Java에서 시간 관련은 어려울 건 없지만,

자주 쓸 일이 없고, 이미 만들어진 걸 가져다 쓰는 정도로만 써서 익숙하지 않았다.

 

그래서 1.8부터 나온 LocalDate, LocalDateTime도 사용해 본 지 얼마 되지 않아서, 정리해본다.

String date_pattern = "yyyy:MM:dd";

LocalDate today = LocalDate.now();
System.out.println("현재 시간 : " + today);
System.out.println("getYear() : " + today.getYear());
System.out.println("getMonth() : " + today.getMonth());
System.out.println("getMonthValue() : " + today.getMonthValue());
System.out.println("getDayOfYear() : " + today.getDayOfYear());
System.out.println("getDayOfMonth(): " + today.getDayOfMonth());

System.out.println();
System.out.println("=========================");
System.out.println("plus date()");
System.out.println("getYear() + 70 : " + (today.getYear() + 70));
System.out.println("plusYears(70) : " + today.plusYears(70).format(DateTimeFormatter.ofPattern(date_pattern)));
System.out.println("getMonthValue() + 70 : " + (today.getMonthValue() + 70));
System.out.println("plusMonths(70) : " + today.plusMonths(70).format(DateTimeFormatter.ofPattern(date_pattern)));
System.out.println("getDayOfMonth() + 70 : " + (today.getDayOfMonth() + 70));
System.out.println("plusDays(70) : " + today.plusDays(70).format(DateTimeFormatter.ofPattern(date_pattern)));

String pattern = "HH:mm:ss";
System.out.println();
System.out.println("LocalDateTime");
System.out.println("=========================");
        
System.out.println("LocalDateTime.now() : " + LocalDateTime.now().format(DateTimeFormatter.ofPattern(pattern)));

LocalDateTime localDateTime = LocalDateTime.now();

//date는 위와 같음
System.out.println("getHour() : " + localDateTime.getHour());
System.out.println("getMinute() : " + localDateTime.getMinute());
System.out.println("getSecond() : " + localDateTime.getSecond());

System.out.println();
System.out.println("=========================");
System.out.println("plus time()");
System.out.println("getHour() + 70 : " + (localDateTime.getHour() + 70));
System.out.println("plusHours(70) : " + localDateTime.plusHours(70).format(DateTimeFormatter.ofPattern(pattern)));
System.out.println("getMinute() + 70 : " + (localDateTime.getMinute() + 70));
System.out.println("plusMinutes(70) : " + localDateTime.plusMinutes(70).format(DateTimeFormatter.ofPattern(pattern)));
System.out.println("getSecond() + 60 : " + (localDateTime.getSecond() + 60));
System.out.println("plusSeconds(100) : " + localDateTime.plusSeconds(100).format(DateTimeFormatter.ofPattern(pattern)));

LocalDateTime customDateTime = LocalDateTime.now().withHour(10).withMinute(10).withSecond(10);
        
System.out.println();
System.out.println("customdate");
System.out.println("=========================");
System.out.println("customdate :: " + customDateTime.getHour() + ":" + customDateTime.getMinute() + ":" + customDateTime.getSecond());
현재 시간 : 2020-10-25
getYear() : 2020
getMonth() : OCTOBER
getMonthValue() : 10
getDayOfYear() : 299
getDayOfMonth(): 25

=========================
plus date()
getYear() + 70 : 2090
plusYears(70) : 2090:10:25
getMonthValue() + 70 : 80
plusMonths(70) : 2026:08:25
getDayOfMonth() + 70 : 95

LocalDateTime
=========================
LocalDateTime.now() : 15:22:44
getHour() : 15
getMinute() : 22
getSecond() : 44

=========================
plus time()
getMinute() + 70 : 92
plusMinutes(70) : 16:32:44
plusSeconds(100) : 15:24:24

customdate
=========================
customdate :: 10:10:10

 

한 번씩 써보면 바로 알 수 있겠지만,

getMonthValue()와  getMonth()의 차이점,

plusYears(70)나 실제로 getYear() + 70 등의 차이만 이해하면 될 것 같다.

반응형