Java获取一天的最大时间23:59:59和最小时间00:00:00
public class Test {
public static void main(String[] args) {
// 加上毫秒数
SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
System.out.println(sDateFormat.format(getAfterYearDateMaxTime(3)));
}
/**
* 若干年之后的23:59:59
*
* @param year
* @return
*/
public static Date getAfterYearDateMaxTime(int year){
Calendar calendarEnd = Calendar.getInstance();
calendarEnd.setTime(new Date());
calendarEnd.add(Calendar.YEAR,year);
calendarEnd.set(Calendar.HOUR_OF_DAY, 23);
calendarEnd.set(Calendar.MINUTE, 59);
calendarEnd.set(Calendar.SECOND, 59);
// MySQL数据库对于毫秒大于500的数据进行进位,所有就造成的MySQL中的时间多一秒,防止mysql自动加一秒,毫秒设为0
calendarEnd.set(Calendar.MILLISECOND, 0);
return calendarEnd.getTime();
}
/**
* 当天的最大时间23:59:59
*
* @return
*/
public static Date getDayMaxTime(){
Calendar calendarEnd = Calendar.getInstance();
calendarEnd.setTime(new Date());
calendarEnd.set(Calendar.HOUR_OF_DAY, 23);
calendarEnd.set(Calendar.MINUTE, 59);
calendarEnd.set(Calendar.SECOND, 59);
// MySQL数据库对于毫秒大于500的数据进行进位,所有就造成的MySQL中的时间多一秒,防止mysql自动加一秒,毫秒设为0
calendarEnd.set(Calendar.MILLISECOND, 0);
return calendarEnd.getTime();
}
/**
* 当天的最小时间00:00:00
*
* @return
*/
public static Date getDayMinTime(){
Calendar calendarEnd = Calendar.getInstance();
calendarEnd.setTime(new Date());
calendarEnd.set(Calendar.HOUR_OF_DAY, 00);
calendarEnd.set(Calendar.MINUTE, 00);
calendarEnd.set(Calendar.SECOND, 00);
// MySQL数据库对于毫秒大于500的数据进行进位,所有就造成的MySQL中的时间多一秒,防止mysql自动加一秒,毫秒设为0
calendarEnd.set(Calendar.MILLISECOND, 0);
return calendarEnd.getTime();
}
}
获取3年后的23:59:59,毫秒数设置成0
2022-09-04 23:59:59.000
最后更新于 2019-09-04 11:22:04 并被添加「java」标签,已有 4 位童鞋阅读过。