赞
踩
判断一个日期是否合法,如2022-02-29、2022-13-01就属于不合法日期
public class TEST2 { public static void main(String[] args) { boolean flag =legallyDate("20220229"); System.out.println(flag); //输出结果为false } public static boolean legallyDate(String date){ //将String类型的日期转换为Integer类型进行判断 int dateInt = Integer.parseInt( date); //取出日期中的年月日分别进行判断 int year = dateInt / 10000; int month = (dateInt % 10000) / 100; int day = dateInt % 100; //定义一个合法月份的天数数组,校验天数是否合法 int [] arr = {31,28,31,30,31,30,31,31,30,31,30,31}; //由于平年二月28天,闰年二月29天,需额外赋值 if((year % 4 == 0 && year %100 != 0) || year % 400 == 0){ arr[1] = 29; //闰年 }else{ arr[1] = 28; //平年 } //校验月份是否合法,0<month<13 if(month >0 && month < 13){ if(day <=arr[month-1] && day>0){ return true; } } return false; } }
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。