求瑞年的java程序,java 计算瑞年的方法
任何語言都有可能計算某一年是否為瑞年的方法,也就是說一年有 366 天,每隔4 年就出現一次。最基本的算法如下: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
知道了這個基本算法,那么起始與語言無關了,這里就用JAVA 語言做一個講解
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
當然,起始用Calendar 也是可以計算出來的.
import java.util.GregorianCalendar;
//...
public boolean isLeapYear(int year) {
GregorianCalendar cal = (
GregorianCalendar) GregorianCalendar.getInstance();
return cal.isLeapYear(year);
}
總結
以上是生活随笔為你收集整理的求瑞年的java程序,java 计算瑞年的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 显示器支架与麻醉机如何搭配使用显示器支架
- 下一篇: 如何操作备份系统还原系统电脑如何还原备份