package step3;

import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date;

/********** Begin / public class TestSomeDate { public static void main(String[] args) throws ParseException { // 输入日期 String inputDate = "2000-5-4"; // 将字符串类型的日期转换为Date类型 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse(inputDate); // 获取年、月、日 Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH); int week = calendar.get(Calendar.DAY_OF_WEEK) - 1; // 判断是否为闰年 boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0; if (isLeapYear) { System.out.println(year + "年是闰年"); } else { System.out.println(year + "年不是闰年"); } // 判断该月有几天 int days = 0; if (month == 2) { if (isLeapYear) { days = 29; } else { days = 28; } } else if (month == 4 || month == 6 || month == 9 || month == 11) { days = 30; } else { days = 31; } System.out.println("这个月有" + days + "天"); // 判断该日是星期几 String[] weeks = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" }; System.out.println("这一天是" + weeks[week]); } } /* End *********/

Java日期计算:判断闰年、月天数和星期几

原文地址: https://www.cveoy.top/t/topic/nIRT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录