HOWTO · Java

Java 요일 가져 오기

이 튜토리얼은 Java에서 요일을 얻는 방법을 보여줍니다.

이 페이지의 내용

이 튜토리얼은 Java에서 요일을 얻는 방법을 보여줍니다.

Java 요일 가져 오기

때때로 UI로 작업하는 동안 요일을 가져와야 하는 요구 사항이 있습니다. 이는 다음 두 가지 방법을 사용하여 Java에서 수행할 수 있습니다.

  1. Java 7부터 SUNDAY(1)에서 SATURDAY(7)까지의 상수를 정의하는 레거시 Calendar 클래스를 사용할 수 있습니다. 캘린더 인스턴스에서 calendar.get(Calendar.DAY_OF_WEEK) 메서드를 사용하여 날짜를 가져올 수 있습니다.
  2. Java 8부터 레거시 API 날짜를 사용하여 월요일(1)부터 일요일(7)까지의 요일을 가져올 수 있습니다. LocalDate.getDayOfWeek() 메소드를 사용하여 요일을 얻을 수 있습니다.

Java 7 Calendar 클래스를 사용하여 요일 가져오기

Java 7 Calendar 클래스는 요일을 가져오는 데 사용됩니다. 이를 위해서는 java.util.Datejava.util.Calendar를 사용해야 합니다.

아래 단계를 따르십시오.

  • Calendar 클래스의 인스턴스를 만듭니다.
  • 현재 날짜를 설정하는 setTime(new Date()) 메서드를 사용하여 달력에 날짜를 설정합니다.
  • 이제 달력 인스턴스에서 get(Calendar.DAY_OF_WEEK)을 사용하여 요일을 가져옵니다.
  • 오늘의 이름을 가져오려면 SimpleDateFormat() 클래스를 사용해야 합니다.

위의 예제를 Java로 구현해 보겠습니다.

package delftstack;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Example {
  public static void main(String[] args) {
    Calendar Demo_Calendar = Calendar.getInstance();
    Demo_Calendar.setTime(new Date());

    int Day_Number = Demo_Calendar.get(Calendar.DAY_OF_WEEK);

    DateFormat Date_Formatter = new SimpleDateFormat("EEEE");
    String Day_Name = Date_Formatter.format(Demo_Calendar.getTime());

    System.out.println("The current day of the Week in number is :: " + Day_Number);
    System.out.println("The current day of the Week in Text is :: " + Day_Name);
  }
}

위의 코드는 현재 날짜의 요일을 숫자와 텍스트로 표시합니다. 출력 참조:

The current day of the Week in number is :: 5
The current day of the Week in Text is :: Thursday

Java 8 날짜 API를 사용하여 요일 가져오기

Java 8 API를 사용하여 요일을 가져오려면 Java의 Time 패키지에서 LocalDate 클래스와 DayofWeek 열거형을 사용해야 합니다. Java 8을 사용하여 요일을 얻으려면 다음 단계를 따르십시오.

  • 현재 날짜가 될 now() 메서드를 사용하여 LocalDate 인스턴스를 만듭니다.
  • LocalDate 인스턴스에서 DayofWeek 열거형 및 getDayOfWeek() 메서드를 사용하여 현재 요일을 가져옵니다.
  • DayofWeek에서 getValue() 메서드를 사용하여 날짜를 가져옵니다.
  • DayofWeek에서 getDisplayName() 메소드를 사용하여 전체 및 짧은 형식으로 요일의 이름을 표시합니다.

위의 예제를 Java로 구현해 봅시다.

package delftstack;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.TextStyle;
import java.util.Locale;

public class Example {
  public static void main(String[] args) {
    LocalDate Current_Date = LocalDate.now();

    DayOfWeek Week_Day = Current_Date.getDayOfWeek();

    System.out.println("The current day of the Week is :: " + Week_Day);
    System.out.println("The current day of the Week in Number is :: " + Week_Day.getValue());
    System.out.println("The current day of the Week in full format is :: "
        + Week_Day.getDisplayName(TextStyle.FULL, Locale.getDefault()));
    System.out.println("The current day of the Week in short format is :: "
        + Week_Day.getDisplayName(TextStyle.SHORT, Locale.getDefault()));
  }
}

위의 코드는 현재 날짜의 요일을 가져와 숫자, 전체 및 짧은 형식으로 변환합니다. 출력 참조:

The current day of the Week is :: THURSDAY
The current day of the Week in Number is :: 4
The current day of the Week in full format is :: Thursday
The current day of the Week in short format is :: Thu