new Date() 方法到底是获取什么时间
2017-11-08 00:00
1011 查看
摘要: 按照通常的理解,new Date()方法调用时,获取的是当前的系统时间。但,真的有这么简单吗?
主方法:
如上图所示,控制台输出的时间,与当前的系统时间一致。
利用win10下的时间设置,更改系统时间后
如上图所示,控制台输出的时间,依然以更新时间前的为基准进行输出。
System.java
由于不确定该时刻是JVM启动时,还是调用 new Date() 方法时;同时为了排除SimpleDateFormat相关方法故继续测试。
要点:确定该时刻,同时排除SimpleDateFormat相关方法影响
说明:在Thread.sleep(10000) ,利用这段时间(10秒),更新系统时间
如上图,更新系统时间前,JVM进程已经启动。
如上图,利用这段时间(10秒),更新系统时间,此时,控制台输出的时间,以更新后的时间为基准进行输出。
但是依然不确定该时刻是调用 new Date() 方法时,还是调用 **System.currentTimeMillis()**时(参见 5、代码溯源 部分 ),故继续测试。
更新测试主方法:
要点:在调用new Date()[b]前,先行调用System.currentTimeMillis()[/b] 方法。
测试方法同上一部分,即:
利用这段时间(10秒),更新系统时间.
现象:该时刻不以调用 System.currentTimeMillis() 时为基准。
因为Date类还有构造Date(long date),用来计算long秒与1970年1月1日之间的毫秒差。得到了这个毫秒数,我们自己也可以算起现在的年月日周时
上述文字,参考:JAVA获取当前时间System.currentTimeMillis()_百度经验
继续查阅Date.java 方法
1、问题
java 中,new Date() 方法,获取的时间,究竟与当前的系统时间有什么关联?简单的时刻对应关系吗?2、测试环境
版本:jdk1.8.0_131主方法:
public static void main(String[] args) throws InterruptedException { SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); int i = 0; while(true){ System.out.println(i + "\t" +dateFormat.format( new Date() ) ); i++; Thread.sleep(2000); } }
3、更新时间前
如上图所示,控制台输出的时间,与当前的系统时间一致。
4、更新时间后
保持该main方法一直在运行(不中断,一直在死循环中),利用win10下的时间设置,更改系统时间后
如上图所示,控制台输出的时间,依然以更新时间前的为基准进行输出。
5、代码溯源
Date.javapackage java.util; public class Date /** * Allocates a <code>Date</code> object and initializes it so that * it represents the time at which it was allocated, measured to the * nearest millisecond. * * @see java.lang.System#currentTimeMillis() */ public Date() { this(System.currentTimeMillis()); } }
System.java
package java.lang; public final class System { /** * Returns the current time in milliseconds. Note that * while the unit of time of the return value is a millisecond, * the granularity of the value depends on the underlying * operating system and may be larger. For example, many * operating systems measure time in units of tens of * milliseconds. * * <p> See the description of the class <code>Date</code> for * a discussion of slight discrepancies that may arise between * "computer time" and coordinated universal time (UTC). * * @return the difference, measured in milliseconds, between * the current time and midnight, January 1, 1970 UTC. * @see java.util.Date */ public static native long currentTimeMillis(); }
6、分析
现象:JVM 在某一时刻,获取当前系统时间,同时内部维护该时间(或者说时刻更为准确),后续所有的时间操作,均以此时刻为基准。由于不确定该时刻是JVM启动时,还是调用 new Date() 方法时;同时为了排除SimpleDateFormat相关方法故继续测试。
7、 继续测试1
更新测试主方法:要点:确定该时刻,同时排除SimpleDateFormat相关方法影响
public static void main(String[] args) throws InterruptedException { int i = 0; System.out.println("开始---" ); Thread.sleep(10000); System.out.println("正式输出---" ); while(true){ System.out.println(i + "\t" + new Date() ); i++; Thread.sleep(2000); } }
说明:在Thread.sleep(10000) ,利用这段时间(10秒),更新系统时间
如上图,更新系统时间前,JVM进程已经启动。
如上图,利用这段时间(10秒),更新系统时间,此时,控制台输出的时间,以更新后的时间为基准进行输出。
8、继续测试2
现象:确定不是在 JVM启动时,内部维护该时刻。但是依然不确定该时刻是调用 new Date() 方法时,还是调用 **System.currentTimeMillis()**时(参见 5、代码溯源 部分 ),故继续测试。
更新测试主方法:
要点:在调用new Date()[b]前,先行调用System.currentTimeMillis()[/b] 方法。
public static void main(String[] args) throws InterruptedException { int i = 0; System.out.println("开始---" ); System.out.println("当前系统时间对应的毫秒数:" + System.currentTimeMillis()); Thread.sleep(10000); System.out.println("正式输出---" ); while(true){ System.out.println(i + "\t" + new Date() ); i++; Thread.sleep(2000); } }
测试方法同上一部分,即:
利用这段时间(10秒),更新系统时间.
现象:该时刻不以调用 System.currentTimeMillis() 时为基准。
9、 分析
System.currentTimeMillis()产生一个当前的毫秒,这个毫秒其实就是自1970年1月1日0时起的毫秒数,Date()其实就是相当于Date(System.currentTimeMillis());因为Date类还有构造Date(long date),用来计算long秒与1970年1月1日之间的毫秒差。得到了这个毫秒数,我们自己也可以算起现在的年月日周时
上述文字,参考:JAVA获取当前时间System.currentTimeMillis()_百度经验
10、猜想
new Date()第一次执行时,获取当前系统时间,同时内部维护该时间(或者说时刻更为准确),后续所有的时间操作,均以此时刻为基准。继续查阅Date.java 方法
private transient long fastTime; public Date() { this(System.currentTimeMillis()); } public Date(long date) { fastTime = date; }
相关文章推荐
- PHP5中Date()函数获取时间相差8小时问题的解决方法
- java使用new Date()和System.currentTimeMillis()获取当前时间戳
- Linux系统date命令的参数及获取时间戳的方法
- js中获取时间new Date()详细介绍
- PHP中Date获取的系统时间不准确的原因和解决方法
- S2SH获取oralce的date类型,显示在web页面带时间方法
- System.currentTimeMillis,getTimeInMillis与new Date().getTime获取当前时间戳耗时比较
- js中获取时间new Date()详细介绍
- 年月日时间格式转时间戳 ,ios系统不兼容 JS 中 = new Date(yyyy-mm-dd)方法
- js的 new Date()日期格式化显示以及js获取时间戳
- java使用new Date()和System.currentTimeMillis()获取当前时间戳
- JS Date当前时间:获取日期时间方法在各浏览器中的差异
- System.currentTimeMillis,getTimeInMillis与new Date().getTime获取当前时间戳耗时比较
- java使用new Date()和System.currentTimeMillis()获取当前时间戳
- JavaScript中获取时间【时间显示不到10自动加0】new Date()_年月日时分秒
- js中获取时间new Date()详细介绍
- Linux系统date命令的参数及获取时间戳的方法
- js中获取时间new Date()详细介绍
- js中获取时间new Date()详细介绍
- System.currentTimeMillis,getTimeInMillis与new Date().getTime获取当前时间戳耗时比较