코딩항해기

[JAVA] 날짜 함수 (JAVA 8 이후) 본문

JAVA

[JAVA] 날짜 함수 (JAVA 8 이후)

miniBcake 2024. 10. 17. 23:50

 

 

JAVA  8 버전 이후로는 time 패키지의 클래스를 사용해 날짜와 시간을 표현할 수 있다.

java.time.LocalDate
java.time.LocalTime
java.time.LocalDateTime

 

java.time.LocalDate

LocalDate 클래스는 날짜를 표현하는 클래스이다.

 

LocalDate.now();

- 현재 날짜를 가져온다.

 

LocalDate.now(ZoneId.of("Europe/Paris"));

날짜를 Europe/Paris의 타임존을 적용해 가져온다.

 

LocalDate 포맷 적용하기

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");

now.format(formatter);

- DateTimeFormatter 클래스를 이용해 원하는 포맷의 문자열로 출력할 수 있다.

 

년, 월(문자열, 숫자), 일, 요일, 일(Year 기준) 출력

public class Client {
    public static void main(String[] args) { 
        // 현재 날짜 구하기        
        LocalDate now = LocalDate.now();        
        // 연도, 월(문자열, 숫자), 일, 일(year 기준), 요일(문자열, 숫자)        
        int year = now.getYear();        
        String month = now.getMonth().toString();        
        int monthValue = now.getMonthValue();        
        int dayOfMonth = now.getDayOfMonth();        
        int dayOfYear = now.getDayOfYear();        
        String dayOfWeek = now.getDayOfWeek().toString();        
        int dayOfWeekValue = now.getDayOfWeek().getValue();        
        // 결과 출력        
        System.out.println(now); // 2021-12-02        
        System.out.println(year); // 2021        
        System.out.println(month + "(" + monthValue + ")"); // DECEMBER(12)       
        System.out.println(dayOfMonth); // 2        
        System.out.println(dayOfYear); // 336        
        System.out.println(dayOfWeek + "(" + dayOfWeekValue + ")"); // THURSDAY(4)
    }
}

 

int year = now.getYear();

- 현재 년도를 가져온다.

 

String month = now.getMonth().toString();

- 해당 월을 가져온다. 리턴받은 Month 객체의 toString() 메소드를 이용하면 월의 이름을 출력하고,

getValue()를 사용하면 숫자로 출력한다.

 

int monthValue = now.getMonthValue();

- 해당 월을 숫자로 표현하기 위해 now.getMonth().getValue()를 사용할 수도 있지만,

LocalDate 객체의 getMonthValue() 메소드를 사용할 수도 있다.

 

int dayOfMonth = now.getDayOfMonth();

- 월의 몇번째 날짜인지를 int로 나타낸다.


int dayOfYear = now.getDayOfYear();

- 년의 몇번째 날짜인지를 int로 나타낸다.


String dayOfWeek = now.getDayOfWeek().toString();

- getDayOfWeek() 메소드는 요일을 나타낸다.

그리고, DayOfWeek 객체의 toString() 메소드를 사용하여, 요일을 텍스트로 출력한다.

 

int dayOfWeekValue = now.getDayOfWeek().getValue();

- DayOfWeek 객체의 getValue() 메소드를 사용해 요일을 숫자로 변환한다.

월요일 (1) ~ 일요일(7)을 리턴한다.

 

 

 

java.time.LocalTime

LocalTime 클래스는 시간을 표현하는 클래스이다.

 

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH시 mm분 ss초");

- Date 예제와 마찬가지로 DateTimeFormatter 클래스를 이용해 원하는 포맷의 문자열로 변환할 수 있다.

 

현재 시간 구하기(시, 분, 초)

public class Client {
    public static void main(String[] args) {
        // 현재 시간
        LocalTime now = LocalTime.now();
        // 현재시간 출력
        System.out.println(now); // 18:03:47.904032
        // 포맷 정의하기
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH시 mm분 ss초");
        // 포맷 적용하기
        String formatedNow = now.format(formatter);
        // 포맷 적용된 현재 시간 출력
        System.out.println(formatedNow); // 18시 03분 47초
        // 현재 시간        
        LocalTime now2 = LocalTime.now();        
        // 현재시간 출력        
        System.out.println(now2); // 18:15:30.193857300        
        // 시, 분, 초 구하기        
        int hour = now2.getHour();        
        int minute = now2.getMinute();        
        int second = now2.getSecond();       
        // 시, 분, 초 출력        
        System.out.println(hour); // 18
        System.out.println(minute); // 15        
        System.out.println(second); // 30
    }
}

 

int hour = now.getHour();
int minute = now.getMinute();
int second = now.getSecond();

- getHour(), getMinute(), getSecond() 메소드를 이용해 시, 분, 초를 각각 구할 수 있다.

 

 

java.time.LocalDateTime

java.time.LocalDateTime 클래스는 날짜와 시간을 표현하는 클래스이다. (LocalDate와 LocalTime을 합친 클래스)

LocalDate, LocalTime 클래스에서 년, 월, 일, 요일, 시, 분, 초를 각각 구했던 것 처럼

LocalDateTime 클래스의 메소드를 이용해서 전부 구할 수 있다.

 

 

 

 

 

출처 블로그: https://dev-coco.tistory.com/31