目录
格式
2021-08-10
YYYY_MM_DD = "yyyy-MM-dd"
获取参数日期的凌晨12点,时间
点击查看代码
/**
* 获取参数日期的凌晨12点,时间字符
*
* @param date 2021-08-10 14:57:37
* @return 示例:2021-08-10 00:00:00
*/
public static Date getDateForZero(Date date){
String format = DateFormatUtils.format(date, YYYY_MM_DD);
return DateUtils.parseDate(format);
}
获取参数日期的凌晨12点,时间字符(日数字不变)
点击查看代码
/**
* 获取参数日期的凌晨12点,时间字符
*
* @param date 2021-08-10 14:57:37
* @return 示例:2021-08-10 00:00:00
*/
public static String getDateForZeroString(Date date){
String format = DateFormatUtils.format(date, YYYY_MM_DD);
/*DateUtils.YYYY_MM_DD;*/
Date newDate = DateUtils.parseDate(format);
return DateFormatUtils.format(newDate, YYYY_MM_DD_HH_MM_SS);
}
获取2个日期的时间差(几天几小时几分)
点击查看代码
/**
* 计算两个时间差
*/
public static String getDatePoor(Date endDate, Date nowDate)
{
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
if(day>0) {
return day + "天" + hour + "小时" + min + "分钟";
}else if(hour>0){
return hour + "小时" + min + "分钟";
}
return min + "分钟";
}
获取当前时间相差n天的凌晨00:00:00时间
点击查看代码
/**
* 获取当前时间相差天数的凌晨00:00:00时间
* @param diffDay
* @return
* @author liuruqi
*/
public static Date getNowDiffDate(int diffDay){
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DATE, diffDay);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
根据日期获取年龄
点击查看代码
/**
* 根据日期获取生日
* @param birthDay
* @return
*/
public static int getAge(Date birthDay) {
Calendar cal = Calendar.getInstance();
if (cal.before(birthDay)) {
return 0;
}
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH);
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
cal.setTime(birthDay);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
int age = yearNow - yearBirth;
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
if (dayOfMonthNow < dayOfMonthBirth)
age--;
} else {
age--;
}
}
return age;
}