参考:计算从现在到凌晨00:00还剩余多少秒

 

应用场景:某些应用需要在特定的时间点更新数据

 1 import java.text.DateFormat;
 2 import java.text.SimpleDateFormat;
 3 import java.util.Calendar;
 4 
 5 public class RemainingTime {
 6   public static void main(String[] args) {
 7     Calendar calendar = Calendar.getInstance();
 8     DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 9     System.out.println("当前时间:" + df.format(calendar.getTime()));
10     // 将日期调整为次日零时,即当前日期加一天,时、分、秒、毫秒都置零。
11     calendar.set(Calendar.MILLISECOND, 0);
12     calendar.set(Calendar.SECOND, 0);
13     calendar.set(Calendar.MINUTE, 0);
14     calendar.set(Calendar.HOUR_OF_DAY, 0);
15     calendar.add(Calendar.DAY_OF_MONTH, 1);
16     System.out.println("次日零时:" + df.format(calendar.getTime()));
17     System.out.println("从现在到凌晨还剩余:" + (calendar.getTimeInMillis() - System.currentTimeMillis()) / 1000 + " s");
18   }
19 }

 

控制台输出:

当前时间:2019-07-08 16:17:50
次日零时:2019-07-09 00:00:00
从现在到凌晨还剩余:27729 s

 

相关文章:

  • 2022-12-23
  • 2021-09-30
  • 2021-11-15
  • 2021-11-27
  • 2021-07-10
猜你喜欢
  • 2022-12-23
  • 2021-07-31
  • 2022-12-23
  • 2022-12-23
  • 2021-09-20
  • 2021-07-22
相关资源
相似解决方案