Determine whether the span of two dates exceeds one year
I wrote this tool class because I encountered a problem when dealing with the query business. The
requirement was 查询的时间范围不能超过十二个月
that I found out that there was no simpler implementation method,
so I thought about it and wrote this tool class by myself.
The first generation product
【Thinking】
- The character string type of the date converted to Date Type
- Then by Date type converted to long millisecond value type (
这是Date记录时间的本质
) - Calculate the number of milliseconds in a year and compare it with the difference between the subtraction of the two parameters
- If the total number of days is greater than 365 days or the second parameter is earlier than the first parameter, it will prompt unreasonable input
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author 二师兄
* @version V1.0
* @Title: 时间校验工具类
* @Description:
* @date 2020/12/30 10:57
*/
public class DateFomart {
public static void main(String[] args) {
//规定需要转换的日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//文本转日期
try {
Date due = sdf.parse("2019-02-26");
Date begin = sdf.parse("2018-02-26");
long time1 = due.getTime();
long time2 = begin.getTime();
long time = time1 - time2;
System.out.println(time + "=" + time1 + "-" + time2);
if(time > 31536000000L || time < 0){
// throw new BillException("查询时间期间不允许超过12个月。");
// System.out.println("查询时间期间不允许超过12个月。");
System.out.println("查询时间期间不合法");
}
} catch (ParseException e) {
e.printStackTrace();
}
}
}
However, due to the existence of leap years, we need to make further judgments.
If there is a leap year that crosses February 29th , then the year is 366 days ,
and it cannot be calculated simply by 365 days.
Second generation product
【Thinking】
- At most one leap year will occur in two adjacent years
开始时间是闰年且月份小于2
, Add 1 day to the original millisecond value (一天是 86400000 毫秒
)- Or
结束时间是闰年且月份大于2
, add 1 day of milliseconds- The above two are to judge whether there is a leap year and whether it crosses the February of the leap year
最开始的思路不是这样的(写着写着发现这样写代码最少)
Insert two lines: (used in the code)
common methods of string classes: https://blog.csdn.net/weixin_44580492/article/details/106026843
time and date common tools: https://blog.csdn.net/ weixin_44580492/article/details/107367202
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author 二师兄
* @version V1.0
* @Title: 时间校验工具类
* @Description:
* @date 2020/12/30 11:42
*/
public class DateCheckUtil {
/**
* 校验日期区间时间跨度是否在一年之内
* 参数日期格式应为 yyyy-MM-dd,例如:2020-12-31
* @param beginDate 开始日期
* @param dueDate 结束日期
*/
public static boolean checkIsOneYear(String beginDate, String dueDate){
//365天的毫秒数
long ms = 31536000000L;
try {
//规定需要转换的日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//文本转日期
Date due = sdf.parse(dueDate);
Date begin = sdf.parse(beginDate);
long time = due.getTime() - begin.getTime();
System.out.println(time + "=" + due.getTime() + "-" + begin.getTime());
//天数大于366天或者小于一天一定属于不合理输入,返回false
if(time > 31622400000L || time < 0L){
// System.out.println("查询时间期间不合法。");
return false;
}
//对字符串截取,取出年份和月份
Integer beginYear = Integer.valueOf(beginDate.substring(0, 4));
Integer beginMonth = Integer.valueOf(beginDate.substring(5, 7));
Integer dueYear = Integer.valueOf(dueDate.substring(0, 4));
Integer dueMonth = Integer.valueOf(dueDate.substring(5, 7));
//判断是否为闰年,并跨过2月,如果是则增加一天的毫秒数
if(isLeapYear(beginYear) && beginMonth <= 2){
ms += 86400000;
}else if(isLeapYear(dueYear) && dueMonth >= 2){
ms += 86400000;
}
return time <= ms;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 给定一个年份,判断是否为闰年
* @param year
* @return
*/
public static boolean isLeapYear(Integer year){
if(year % 100 == 0){
if(year % 400 == 0){
return true;
}
}else if(year % 4 == 0){
return true;
}
return false;
}
public static void main(String[] args) {
String begin = "2020-01-15";
String dueDate = "2021-01-15";
boolean b = checkIsOneYear(begin, dueDate);
System.out.println(b);
}
}