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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值