자바8의 Time 및 Date 관련 새로운 API를 공부하게 되어 정리하게 되었다. 



1. LocalDate, LocalTime, LocalDateTime, DateTimeFormatter 사용하기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package dateTimeTest;
 
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Locale;
 
public class LocalDateTimeTest {
 
    public static void main(String[] args) {
 
        // 1. LocalDate 사용
        LocalDate today = LocalDate.now();
        System.out.println(today);
        
        System.out.println("년월일"+today.getYear() + "/" + today.getMonth() + " "+today.getMonthValue() 
        +"/"+today.getDayOfMonth() + " 지난일자 " + today.getDayOfYear());
        System.out.println("요일 " + today.getDayOfWeek()+" "+today.getDayOfWeek().getValue());
        
        // 특정 날짜를 지정해서 LocalDate 생성
        LocalDate endDay = LocalDate.of(20191231);
        System.out.println("현재 기준 몇일 남아 있는지 "+ today.until(endDay, ChronoUnit.DAYS));
        
        System.out.println("현재 기준 1개월 후 "+today.plusMonths(1));
        System.out.println(DayOfWeek.TUESDAY.plus(3));
        
        // LocalTime 사용
        LocalTime now = LocalTime.now();
        System.out.println(now);
        System.out.println("시분초나노초" + now.getHour() + " " + now.getMinute()
                                        + " " + now.getSecond() + " " +now.getNano());
        
        // 특정 시간을 지정해서 LocalTime 생성
        LocalTime bedTime = LocalTime.of(2340);
        LocalTime wakeTime = bedTime.plusHours(8);
        System.out.println(wakeTime);
        
        // LocalDateTime 사용
        LocalDateTime dt = LocalDateTime.now();
        System.out.println(dt);
 
        LocalDate date = dt.toLocalDate();
        System.out.println(date);
        
        LocalTime time = dt.toLocalTime();
        System.out.println(time);
        
        // 특정 날짜와 시간을 지정해서 LocalDateTime 생성
        LocalDateTime dt2 = LocalDateTime.of(2019,2,19,13,20,50);
        System.out.println(dt2);
        System.out.println(dt2.getMonth());
        
        dt2.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        
        // 포맷을 직접 설정한 Formatter 생성
        DateTimeFormatter myFormat = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss E a", Locale.KOREAN);
        System.out.println(dt2.format(myFormat));    
    }
}
cs





2. LocalDateTime 응용해 현재로부터 월급날까지 남은 일수 계산하기



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package dateTimeTest;
 
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
 
public class SalaryDate {
    
    // 내 월급날(매월 25일)은 몇일 남았을까?
    public static void main(String[] args) {
         LocalDate today = LocalDate.now();
//        LocalDate today = LocalDate.of(2019, 2, 26);
        
        // 1. 기준이 되는 날짜를 구하기
        LocalDate theDay = LocalDate.from(today);
        System.out.println("오늘날짜 : "+theDay);
        
        // 2. 월급날 구하기 매월 25일 
        int salaryDay = 25;
        int remainDay = 0;
 
        if(theDay.getDayOfMonth() > salaryDay) {
            LocalDate lastDayOfMonth = theDay
                    .with(TemporalAdjusters.lastDayOfMonth());
            remainDay = lastDayOfMonth.getDayOfMonth()-theDay.getDayOfMonth()+salaryDay;
        }else if(theDay.getDayOfMonth() < salaryDay) {
            remainDay = salaryDay-theDay.getDayOfMonth();
        }else {
            remainDay = 0;
        }
        
        System.out.println("월급까지 "+remainDay+"일 남았습니다.");
    }
}
 
cs


1
2
오늘날짜 : 2019-02-20
월급까지 5일 남았습니다.
cs



소스코드 첨부: 


myLamdaTest.zip




+ Recent posts