使用场景:redis缓存需要设置 键-值 的过期时间的时候,我们会遇到这个问题。
使用方法:方案一: 使用Calendar(Java 8之前)
public static Integer getRemainSecondsOneDay(Date currentDate) {
Calendar midnight=Calendar.getInstance();
midnight.setTime(currentDate);
midnight.add(midnight.DAY_OF_MONTH,1);
midnight.set(midnight.HOUR_OF_DAY,0);
midnight.set(midnight.MINUTE,0);
midnight.set(midnight.SECOND,0);
midnight.set(midnight.MILLISECOND,0);
Integer seconds=(int)((midnight.getTime().getTime()-currentDate.getTime())/1000);
return seconds;
}
方案二:使用LocalDateTime(java 8)
public static Integer getRemainSecondsOneDay(Date currentDate) {
Lo