首页 > 编程语言 > 详细

java的 System.currentTimeMillis() 与当前计算机时间

时间:2020-11-12 13:25:52      阅读:83      评论:0      收藏:0      [点我收藏+]

System.currentTimeMillis() 获取的是返回当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数。

当前计算机时间是跟你的计算机所在时区是有关的!!!

故当前计算机时间和System.currentTimeMillis()所在时区可能不一样,会相差一些小时【中国东八区相差8小时】,这点使用时要注意。最好使用 java.util.Calendar 可以设置时区。

 

 

public static void main(String[] args) {
    int   currentDay=TimeDateUtil.getCurrentDay();
  long currentTime = currentDay*TimeDateUtil.ONEDAY;
  long tomorrowZeroTime =currentTime + TimeDateUtil.ONEDAY*2;
  int todayZeroTimeReturnInt = TimeDateUtil.getTodayZeroTimeReturnInt();
  //todayZeroTimeReturnInt:1605024000,currentDay:18576,currentTime:1604966400000
  System.out.println("todayZeroTimeReturnInt:"+todayZeroTimeReturnInt+",currentDay:"+currentDay+",currentTime:"+currentTime+",currentSystemTime:"+System.currentTimeMillis());
  System.out.println("nowZeroTime:"+String.format("%tc", todayZeroTimeReturnInt*1000L));//当前时间的凌晨时间
  System.out.println("systemCurrentTime:"+String.format("%tc", System.currentTimeMillis()));
  System.out.println("currentTime:"+String.format("%tc", currentTime));
  System.out.println("tomorrowZeroTime:"+String.format("%tc", tomorrowZeroTime));
}
/** * 时间日期工具类 * @author 哈皮 * */ public class TimeDateUtil { /** * 一天的时间 */ public static final long ONEDAY = 3600*24*1000; /**当前总天*/ public static int getCurrentDay(){ return getTodayZeroTimeReturnInt()/ONE_DAY; } /** * 获取今天零时的时间 * @return 零时以int类型返回的数值 */ public static int getTodayZeroTimeReturnInt(){ long todayZeroTime = getTodayZeroTime(); return (int) (todayZeroTime/1000); } /** * 获取今天零时的时间 * @return 零时以long类型返回的数值 */ public static long getTodayZeroTime(){ long now = System.currentTimeMillis(); long more = now%ONEHOUR; int nowHour = getHour(); long zeroTime = now-(nowHour*ONEHOUR+more); return zeroTime; } }

 

 

控制台输出:

todayZeroTimeReturnInt:1605110400,currentDay:18577,currentTime:1605052800000,currentSystemTime:1605147353125
nowZeroTime:周四 11月 12 00:00:00 CST 2020
systemCurrentTime:周四 11月 12 10:15:53 CST 2020
currentTime:周三 11月 11 08:00:00 CST 2020
tomorrowZeroTime:周五 11月 13 08:00:00 CST 2020

 

会发现 1605110400/86400000 其实是除不尽的,会有余数。

 

java的 System.currentTimeMillis() 与当前计算机时间

原文:https://www.cnblogs.com/erlongxizhu-03/p/13962545.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!