计算天数的工具类设计:
/**
* 计算两个时间相差的天数
*
* @param endDate
* @param nowDate
* @return
*/
public static Integer getDatePoorDay(Date endDate, Date nowDate) {
long nd = 1000 * 24 * 60 * 60;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少天
long day = diff / nd;
return Integer.valueOf(day + "");
}
使用案例分析:
计算使用的设备距离过期时间相差的天数,即过期时间距今天相差天数
import java.util.Calendar;
// 获取设备的过期时间
Date expiredTime = getExpiredTime();
// 获取今日的date(24小时制的0点0分0秒)
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
// 算出过期时间相差天数
Integer expiredDay = DateUtils.getDatePoorDay(expiredTime, calendar.getTime());
2、根据用户的出生日期计算年龄
计算用户年龄的工具类设计:
/**
* 计算用户的年龄
*
* @param birthday 出生日期
* @return 结果
*/
public static Long calcUserAge(Date birthday) {
if(Objects.isNull(birthday)) {
return null;
}
// 计算用户的年龄
LocalDate today = LocalDate.now();
LocalDate birthdayLocalDate = Instant.ofEpochMilli(birthday.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
Long years = ChronoUnit.YEARS.between(birthdayLocalDate, today);
return years;
}
使用案例分析:
用户出生日期:1993-03-13,计算用户的年龄 --> 28
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date birthday = sdf.parse("1993-03-13");
Long age = DateUtils.calcUserAge(birthday);
System.out.println("用户年龄为:" + age);
} catch (ParseException e) {
e.printStackTrace();
}
3、获取当天时间的0点
获取当天时间0点的工具类设计:
/**
* 获取当天零点
* @return
*/
public static Date getTodayZeroPoint(){
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
注意点:
Calendar.HOUR 表示12小时制,若不指定Calendar.AM_PM,则会根据当前的时间,设置0点,比如:当前时间是上午(AM),设置的0点就是早上 00:00:00; 若是当前时间是下午(PM),则设置的0点就是 12:00:00
4、计算2个时间是否相距2个小时
工具类设计:
/**
* 计算两个时间差(秒)
*
* @param beginTime
* @param endTime
* @return
*/
public static long calTimeDiffSecond(Date beginTime, Date endTime){
long diffSecond = (endTime.getTime() - beginTime.getTime()) / 1000;
return diffSecond;
}
使用案例分析:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date1 = sdf.parse("2021-12-23 16:00:00");
Date date2 = sdf.parse("2021-12-23 17:00:00");
Date compareDate = sdf.parse("2021-12-23 19:00:00");
long diffSecond = DateUtils.calTimeDiffSecond(date1, compareDate);
System.out.println("date1是否间隔compareDate,2个小时:" + (diffSecond <= 7200));
long diffSecond2 = DateUtils.calTimeDiffSecond(date2, compareDate);
System.out.println("date2是否间隔compareDate,2个小时:" + (diffSecond2 <= 7200));
} catch (ParseException e) {
e.printStackTrace();
}
5、获取指定日期增加 或 减少对应天数的时间
工具类设计:
/**
* 获取当前时间往前或往后移除一定天数的时间
*
* @param day 正数为往后加几天 负数为往前加几天
* @return
*/
public static Date getTimeByDay(int day){
Date date = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
//把日期往前或往后增加天数,整数 往后推,负数往前移动
calendar.add(Calendar.DATE, day);
date = calendar.getTime();
return date;
}
使用案例:
String deviceBindDate = "2021-12-24";
// 增加2天
Date timeAddDay = DateUtils.getTimeByDay(2);
// 减少2天
Date timeSubtractDay = DateUtils.getTimeByDay(-2);
System.out.println("timeAddDay:" + timeAddDay.toLocaleString());
System.out.println("timeSubtractDay:" + timeSubtractDay.toLocaleString());