Skip to content Skip to footer

「Java案例」判断是否是闰年的方法

定义方法实现闰年判断与天数计算

闰年判断与天数计算实现

编写一个程序,要求编写方法public static boolean isLeapYear(int year)实现判断闰年方法;编写方法public static int numberOfDays(int year)计算一年有多少天。

java

复制代码

# 源文件保存为"YearCalculator.java"

public class YearCalculator {

public static void main(String[] args) {

int testYear = 2024;

System.out.println(testYear + "年是闰年吗? " + isLeapYear(testYear));

System.out.println(testYear + "年有 " + numberOfDays(testYear) + " 天");

}

// 判断是否为闰年

public static boolean isLeapYear(int year) {

if (year % 4 != 0) {

return false;

} else if (year % 100 != 0) {

return true;

} else {

return year % 400 == 0;

}

}

// 计算一年的天数

public static int numberOfDays(int year) {

return isLeapYear(year) ? 366 : 365;

}

}

运行结果

yaml

复制代码

2024年是闰年吗? true

2024年有 366 天

代码解析:

isLeapYear方法遵循闰年判断规则:

不能被4整除的不是闰年

能被4整除但不能被100整除的是闰年

能被100整除但不能被400整除的不是闰年

能被400整除的是闰年

numberOfDays方法直接利用isLeapYear的结果返回365或366天

相关案例解析

计算某年某月的天数

编写一个程序,要求编写方法public static boolean isLeapYear(int year)实现判断闰年方法;编写方法public static int getMonthDays(int year, int month)计算某年某月的天数。

java

复制代码

# 源文件保存为"MonthDays.java"

public class MonthDays {

public static void main(String[] args) {

int year = 2024;

int month = 2;

System.out.println(year + "年" + month + "月有 " + getMonthDays(year, month) + " 天");

}

public static int getMonthDays(int year, int month) {

switch (month) {

case 1: case 3: case 5: case 7: case 8: case 10: case 12:

return 31;

case 4: case 6: case 9: case 11:

return 30;

case 2:

return isLeapYear(year) ? 29 : 28;

default:

return -1; // 无效月份

}

}

public static boolean isLeapYear(int year) {

return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

}

}

运行结果

yaml

复制代码

2024年2月有 29 天

代码解析:通过switch-case结构处理各月份天数,2月份根据闰年判断返回28或29天。

计算某日期是该年的第几天

编写一个程序,要求编写方法public static boolean isLeapYear(int year)实现判断闰年方法;编写方法public static int getMonthDays(int year, int month)计算某年某月的天数;编写方法public static int getDayOfYear(int year, int month, int day)计算某日期是该年的第几天。

java

复制代码

# 源文件保存为"DayOfYear.java"

public class DayOfYear {

public static void main(String[] args) {

int year = 2024;

int month = 3;

int day = 15;

System.out.println(year + "年" + month + "月" + day + "日是当年的第 "

+ getDayOfYear(year, month, day) + " 天");

}

public static int getDayOfYear(int year, int month, int day) {

int totalDays = day;

for (int m = 1; m < month; m++) {

totalDays += getMonthDays(year, m);

}

return totalDays;

}

public static int getMonthDays(int year, int month) {

switch (month) {

case 1: case 3: case 5: case 7: case 8: case 10: case 12:

return 31;

case 4: case 6: case 9: case 11:

return 30;

case 2:

return isLeapYear(year) ? 29 : 28;

default:

return -1; // 无效月份

}

}

public static boolean isLeapYear(int year) {

return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

}

}

运行结果

yaml

复制代码

2024年3月15日是当年的第 75 天

代码解析:累加当前月份之前所有月份的天数,再加上当前月份已过天数。

判断两个日期之间的天数差

编写一个程序,要求编写方法public static boolean isLeapYear(int year)实现判断闰年方法;编写方法public static int getMonthDays(int year, int month)计算某年某月的天数;编写方法public static int getDayOfYear(int year, int month, int day)计算某日期是该年的第几天;编写方法public static int daysBetween计算两个日期之间的天数差。

java

复制代码

# 源文件保存为"DateDifference.java"

public class DateDifference {

public static void main(String[] args) {

int year1 = 2024, month1 = 3, day1 = 15;

int year2 = 2024, month2 = 5, day2 = 20;

System.out.println("两个日期相差 " +

daysBetween(year1, month1, day1, year2, month2, day2) + " 天");

}

public static int daysBetween(int y1, int m1, int d1, int y2, int m2, int d2) {

return Math.abs(getDayOfYear(y2, m2, d2) - getDayOfYear(y1, m1, d1));

}

public static int getDayOfYear(int year, int month, int day) {

int totalDays = day;

for (int m = 1; m < month; m++) {

totalDays += getMonthDays(year, m);

}

return totalDays;

}

public static int getMonthDays(int year, int month) {

switch (month) {

case 1: case 3: case 5: case 7: case 8: case 10: case 12:

return 31;

case 4: case 6: case 9: case 11:

return 30;

case 2:

return isLeapYear(year) ? 29 : 28;

default:

return -1; // 无效月份

}

}

public static boolean isLeapYear(int year) {

return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

}

}

运行结果

复制代码

两个日期相差 66 天

代码解析:计算两个日期各自在当年的天数差,取绝对值得到间隔天数。

操作练习题

判断生日是否在闰日

编写一个程序,要求编写方法public static boolean isLeapYear(int year)实现判断闰年方法;编写方法public static boolean isLeapDayBirthday(int year, int month, int day)判断给定的生日是否是2月29日,且出生年份确实是闰年。 参考代码:

java

复制代码

# 源文件保存为"LeapDayBirthday.java"

public class LeapDayBirthday {

public static void main(String[] args) {

int year = 2000;

int month = 2;

int day = 29;

System.out.println(isLeapDayBirthday(year, month, day));

}

public static boolean isLeapDayBirthday(int year, int month, int day) {

return month == 2 && day == 29 && isLeapYear(year);

}

public static boolean isLeapYear(int year) {

return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

}

}

运行结果

arduino

复制代码

true

代码解析:同时检查日期是否为2月29日且年份为闰年。

计算从某年到某年之间的闰年数量

编写一个程序,要求编写方法public static boolean isLeapYear(int year)实现判断闰年方法;编写方法public static int countLeapYears(int start, int end)计算两个年份之间的闰年数量。 参考代码:

java

复制代码

# 源文件保存为"CountLeapYears.java"

public class CountLeapYears {

public static void main(String[] args) {

int start = 2000;

int end = 2025;

System.out.println(start + "年到" + end + "年之间有 "

+ countLeapYears(start, end) + " 个闰年");

}

public static int countLeapYears(int start, int end) {

int count = 0;

for (int year = start; year <= end; year++) {

if (isLeapYear(year)) count++;

}

return count;

}

public static boolean isLeapYear(int year) {

return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

}

}

运行结果

yaml

复制代码

2000年到2025年之间有 7 个闰年

代码解析:遍历两个年份之间的每一年,统计闰年数量。

计算某年某月的星期五数量

编写一个程序,要求编写方法public static int countFridays(int year, int month) 计算指定年份和月份中星期五的天数。

参考代码:

java

复制代码

# 源文件保存为"FridayCount.java"

import java.time.LocalDate;

public class FridayCount {

public static void main(String[] args) {

int year = 2025;

int month = 5;

System.out.println(year + "年" + month + "月有 "

+ countFridays(year, month) + " 个星期五");

}

public static int countFridays(int year, int month) {

int count = 0;

LocalDate date = LocalDate.of(year, month, 1);

int daysInMonth = date.lengthOfMonth();

for (int day = 1; day <= daysInMonth; day++) {

if (LocalDate.of(year, month, day).getDayOfWeek().getValue() == 5) {

count++;

}

}

return count;

}

}

运行结果

yaml

复制代码

2025年5月有 5 个星期五

代码解析:使用Java 8的日期API,遍历月份中的每一天,检查是否为星期五。

Copyright © 2088 上届世界杯冠军_u20世界杯八强 - longxinwl.com All Rights Reserved.
友情链接