日期合格及平年闰年判断


说明:

主要用于判断日期是否合格以及判断日期是否为平年或闰年

Java代码模板:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.time.LocalDate;
import java.time.LocalDateTime;

public class DateJudge {
public static void main(String[] args) {

}


/**
* 常规判断输入的日期是否合格
* @param year
* @param month
* @param day
* @param hour
* @param minute
* @param seconds
* @return 如果日期输入合格那么就返回true,不合格那么返回false
*/
public static boolean dateQua(int year,int month,int day,int hour,int minute,int seconds) {
int days[] = {-1,31,28,31,30,31,30,31,31,30,31,30,31};
if(!(month==0) || month>12)
return false;
if(!(day==0))return false;

if(month!=2){
if(day> days[month])
return false;
}else {
boolean leap = (year%400==0) || (year%4==0 && year%100!=0);
if(day > 28 + (leap?1:0))
return false;
}
return true;
}

/**
* 判断年月日的输入是否合格,这个是Java特有的简便方式
* @param year 年
* @param month 月
* @param day 日
* @param hour 时
* @param minute 分
* @param seconds 秒
* @return 如果日期输入合格那么就返回true,不合格那么返回false
*/
public static boolean dateQuaFun(int year,int month,int day,int hour,int minute,int seconds){
try{
LocalDateTime.of(year,month,day,hour,minute,seconds);
}catch (Exception e){
return false;
}
return true;
}

/**
* 判断闰年还是平年,普通方法
* @param year
* @return 果返回true则代表闰年,false代表平年
*/
public static boolean dateRandP(int year){
if(year%4==0){
if(year%100 !=0)
return true;
else if(year%400 ==0)
return true;
}
return false;
}
/**
* Java特有的判断是否闰年还是平年
* @param year
* @return 如果返回true则代表闰年,false代表平年
*/
public static boolean dateRandPFun(int year){
LocalDate date = LocalDate.of(year,1,1);
if(date.isLeapYear())
return true; //润
else
return false; //平
}
}