JAVA/포스팅

java.time.Month 클래스

짜집퍼박사(짜박) 2023. 11. 17. 13:46

java.time.Month 클래스는 월을 나타내는 열거형으로, Java 8에서 도입된 java.time 패키지에 속합니다. 이 클래스는 1월부터 12월까지의 상수를 제공합니다.

 

1. Month 상수

Month 열거형은 다음과 같은 상수를 가지고 있습니다.

 

- JANUARY: 1월
- FEBRUARY: 2월
- MARCH: 3월
- APRIL: 4월
- MAY: 5월
- JUNE: 6월
- JULY: 7월
- AUGUST: 8월
- SEPTEMBER: 9월
- OCTOBER: 10월
- NOVEMBER: 11월
- DECEMBER: 12월

 

Month 사용 예제

import java.time.Month;
import java.time.LocalDate;

public class MonthExample {
    public static void main(String[] args) {
        // 현재 날짜를 가져오기
        LocalDate currentDate = LocalDate.now();

        // 현재 날짜의 월 가져오기
        Month currentMonth = currentDate.getMonth();

        // 출력
        System.out.println("Current Date: " + currentDate);
        System.out.println("Month: " + currentMonth);
    }
}

이 예제에서는 LocalDate를 사용하여 현재 날짜를 가져오고, getMonth() 메서드를 사용하여 해당 날짜의 월을 가져옵니다.

 

2. Month 메서드

Month 클래스에는 다양한 메서드가 있습니다. 몇 가지 주요 메서드는 다음과 같습니다.

- getValue(): 해당 월에 대한 숫자 값을 반환합니다. 1월부터 12월까지 각각 1부터 12까지의 값을 가집니다.
- name(): 해당 월의 이름을 문자열로 반환합니다.
- maxLength(): 해당 월의 최대 일수를 반환합니다.

 

Month 메서드 사용 예제

import java.time.Month;

public class MonthMethodsExample {
    public static void main(String[] args) {
        // Month 열거형의 메서드 사용
        Month january = Month.JANUARY;

        System.out.println("Value: " + january.getValue());         // 1
        System.out.println("Name: " + january.name());               // JANUARY
        System.out.println("Max Length: " + january.maxLength());    // 31
    }
}

이 예제에서는 Month.JANUARY 상수를 사용하여 Month 객체를 생성하고, 다양한 메서드를 사용하여 값을 출력합니다.

 

With ChatGPT

'JAVA > 포스팅' 카테고리의 다른 글

java.time.Year 클래스  (0) 2023.11.17
java.time.MonthDay 클래스  (0) 2023.11.17
자바 java.time DayOfWeek  (0) 2023.11.17
DateTimeFormatter 클래스  (0) 2023.11.17
java.time.Period 클래스  (0) 2023.11.17