본문 바로가기
슬기로운 자바 개발자 생활/Java more

자바로 윤년 체크하기

by 슬기로운 동네 형 2023. 3. 29.
반응형

Why is there a Leap Year?

Leap Day

 윤년(閏年, leap year)은 과년(夥年)이라고도 하며, 역법인 태음력이나 태양력에서, 자연의 흐름에 대해서 생길 수 있는 오차를 보정하기 위해 삽입하는 날이나 주, 달이 들어가는 해를 말한다. 삽입하는 달을 정하는 방법을 치윤법(置閏法)이라고 한다.

 한국법에서는, "윤년"이란 그레고리력에서 여분의 하루인 2월 29일을 추가하여 1년 동안 날짜의 수가 366일이 되는 해를 말한다(천문법 제2조 제5호). 윤년에는 2월과 8월이 같은 요일로 시작된다.

 

간단하게 그 해의 달력 2월 29일이 있다면 윤년이고, 2월 28일까지 있다면 평년이다. 즉 365일은 평년, 366일은 윤년이다.

4년마다 한 번씩 돌아온다.

  • 4로 나누어 떨어지는 해는 윤년, 그 밖의 해는 평년으로 한다.
  • 100으로 나누어 떨어지되 400으로 나누어 떨어지지 않는 해는 평년으로 한다.
package com.company;

import java.util.GregorianCalendar;

/**
 * 자바로 윤년을 구하기
 * 해당 년도를 4로 나눈 값이 0인 것을 의미.
 * 그 중 100으로 나눈 값이 0인 것은 윤년에서 제외
 * 400으로 나눈 값이 0인 것은 윤년으로 포함. 그외는 모두 평년으로 판단
 */
public class LeapYearCheck {
    public static void getLeapYear(){
        for(int year=2010; year < 2031 ; year++){
            if(year % 4==0 && year % 100 !=0 || year % 400 ==0){
                System.out.println(year+" 은 윤년 입니다.");
            }else{
                System.out.println(year+" 은 평년 입니다.");
            }
        }

    }
    public static void main(String[] args) {
        LeapYearCheck.getLeapYear();

        int year = 2024;
        GregorianCalendar gc = new GregorianCalendar();
        if(gc.isLeapYear(year)){
            System.out.println(year+" 은 윤년 입니다.");
        }else{
            System.out.println(year+" 은 평년 입니다.");
        }

    }
}
GregorianCalendar

 GregorianCalendat클래스의 isLeapYear()를 사용하면 간단하게 윤년을 구할 수 있다.

 가끔씩 연체료나 이자금액을 구하는 프로그램을 만들 때, 윤년을 구해서 365일 또는 366일을 구하곤 한다.

 자바에서는 해당 클래스가 있으므로 굳이 계산할 필요가 없다.

 

https://namu.wiki/w/%EC%9C%A4%EB%85%84

 

윤년 - 나무위키

이 문서의 내용 중 전체 또는 일부는 다른 문서에서 가져왔습니다. [ 펼치기 · 접기 ] 문서의 r115 판 (이전 역사) 문서의 r 판 (이전 역사) 율리우스력의 윤년 추가 규칙은 다음과 같다. 4로 나누어

namu.wiki

 

Ever wondered why every four years, we get an extra day in February? 

Our calendar system is close to perfect, but not completely. We need leap years to keep our modern-day Gregorian calendar in alignment with Earth’s revolutions around the Sun. The Gregorian calendar, used by most of the world today, was named after Pope Gregory XIII, who introduced it in October 1582. This new calendar replaced the Julian one to stop the drift of the calendar with respect to the equinoxes

Without leap days, our calendar would be off by 1 day every 4 years. In fact, we lose almost 6 hours every single year, and after 100 years that would result in about 24 days lost!

According to Timeanddate.com, it takes Earth approximately 365.242189 days, or 365 days, 5 hours, 48 minutes, and 45 seconds, to circle once around the Sun. This full orbit of Earth around the Sun is known as a tropical year. And our whole calendar system is based and measured around it.

반응형

댓글