Java – うるう年の計算方法
うるう年は、追加の1日(年366日)を含む年です。
うるう年のアルゴリズムを見直してください:
if year is divisible by 400 then is__leap__year else if year is divisible by 100 then not__leap__year else if year is divisible by 4 then is__leap__year else not__leap__year
P.Sアルゴリズムhttp://ja.wikipedia.org/wiki/Leap
year[wikipediaうるう年].__
1. Java Leap Yearの例
指定された年がうるう年かどうかを判断するJavaの例
DateTimeExample.java
package com.mkyong.utils; public class DateTimeExample { public static void main(String[]args) { DateTimeExample obj = new DateTimeExample(); System.out.println("1993 is a leap year : " + obj.isLeapYear(1993)); System.out.println("1996 is a leap year : " + obj.isLeapYear(1996)); System.out.println("2012 is a leap year : " + obj.isLeapYear(2012)); } public boolean isLeapYear(int year) { if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) { return true; } else { return false; } } }
出力
1993 is a leap year : false 1996 is a leap year : true 2012 is a leap year : true
2. GregorianCalendarの例
代わりに、
GregorianCalendar.isLeapYear()
APIを使うこともできます。
import java.util.GregorianCalendar; //... public boolean isLeapYear(int year) { GregorianCalendar cal = ( GregorianCalendar) GregorianCalendar.getInstance(); return cal.isLeapYear(year); }
参考文献
-
http://en.wikipedia.org/wiki/Gregorian__calendar
[Wikipedia:グレゴリオ暦
カレンダー]。
http://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html
[GregorianCalendar
Java doc]
リンク://タグ/うるう年/[うるう年]