Java에서 Zellers 합동을 사용하여 요일 찾기
MD Aminul Islam
2023년10월12일
Java
Java Date

이 기사에서는 Java를 사용하여 Zeller의 합동을 구현하여 요일을 찾는 방법을 보여줍니다. 또한 주제를 더 쉽게 설명하기 위해 한 줄씩 설명이 포함된 예를 살펴보겠습니다.
Java에서 Zeller의 합동을 사용하여 요일 찾기
이 문서에서 사용하는 알고리즘은 이전 연도의 1월
을 13
으로, 2월
을 14
로 계산하는 것입니다.
예를 들어 날짜가 2009년 1월 13일
인 경우 알고리즘은 2008년 13번째 달
로 계산합니다. 다음 코드 펜스에서는 일주일 동안 요일을 찾는 방법을 보여줍니다.
예제 코드:
public class FindDay {
// A method to print a day for a Date
static void ZellerCongruence(int Day, int Month, int Year) {
if (Month == 1) { // Checking the month if it's "January"
Month = 13;
Year--;
}
if (Month == 2) { // Checking the month if it's "February"
Month = 14;
Year--;
}
int DD = Day;
int MM = Month;
int yy = Year % 100;
int YY = Year / 100;
// Calculating the day
int Calc = DD + 13 * (MM + 1) / 5 + yy + yy / 4 + YY / 4 + 5 * YY;
Calc = Calc % 7; // Finding the day
switch (Calc) {
case 0:
System.out.println("The day is: Saturday");
break;
case 1:
System.out.println("The day is: Sunday");
break;
case 2:
System.out.println("The day is: Monday");
break;
case 3:
System.out.println("The day is: Tuesday");
break;
case 4:
System.out.println("The day is: Wednesday");
break;
case 5:
System.out.println("The day is: Thursday");
break;
case 6:
System.out.println("The day is: Friday");
break;
}
}
// Our main class
public static void main(String[] args) {
ZellerCongruence(20, 9, 2022); // The date format is (dd/mm/yyyy)
}
}
우리는 이미 각 행의 목적을 설명했습니다. 따라서 위의 예제 코드를 실행하면 콘솔에 아래와 같은 출력이 표시됩니다.
The day is: Tuesday
튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다
작가: MD Aminul Islam
Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.
LinkedIn