Spring boot 项目 通过日志记录插入sql操作用时

long start2 = System.currentTimeMillis();
getDao().batchInsert(batchList);
long end2 = System.currentTimeMillis();
log.info("Save {} data 2 DB successfully took time: {}", getDescName(), (end2 - start2));

怎么查看日志发现有用时为负数的情况呢?

2019-05-16 14:41:04.420  INFO 3324 --- [ave2db-thread-2] c.c.sz_vss.demo.AbstractSave2DBProcess   : Save Stock data 2 DB batch size: 416
2019-05-16 14:41:03.152  INFO 3324 --- [ave2db-thread-2] c.c.sz_vss.demo.AbstractSave2DBProcess   : Save Stock data 2 DB successfully took time: -1268

怎么用时-1268毫秒呢? Windows系统的时间偶尔会自动回拨吗?

正常情况下应该很少发生吧.

通常自动更新时间时有可能产生这个问题

按照jdk的文档计算时差不建议使用System.currentTimeMillis()

你可以看下说明.System.nanoTime()是jdk推荐的方式,即使时间回拨了也不受影响,它是从开机开始计算的时间.

而且它建议的时间差是做减法而不是比大小.

/**
long java.lang.System.nanoTime() Returns the current value of the running Java Virtual Machine's high-resolution time source, in nanoseconds. This method can only be used to measure elapsed time and is not related to any other notion of system or wall-clock time. The value returned represents nanoseconds since some fixed but arbitrary origin time (perhaps in the future, so values may be negative). The same origin is used by all invocations of this method in an instance of a Java virtual machine; other virtual machine instances are likely to use a different origin. This method provides nanosecond precision, but not necessarily nanosecond resolution (that is, how frequently the value changes) - no guarantees are made except that the resolution is at least as good as that of currentTimeMillis(). Differences in successive calls that span greater than approximately 292 years (263 nanoseconds) will not correctly compute elapsed time due to numerical overflow. The values returned by this method become meaningful only when the difference between two such values, obtained within the same instance of a Java virtual machine, is computed. For example, to measure how long some code takes to execute:

long startTime = System.nanoTime();

// ... the code being measured ...

long estimatedTime = System.nanoTime() - startTime; To compare two nanoTime values

long t0 = System.nanoTime();

...

long t1 = System.nanoTime();

one should use t1 - t0 < 0, not t1 < t0, because of the possibility of numerical overflow.Returns:the current value of the running Java Virtual Machine's high-resolution time source, in nanosecondsSince:1.5

**/

原文地址:https://www.oschina.net/question/1175066_2306338

Windows系统时间会偶尔自动回拨吗?的更多相关文章

  1. Windows系统时间(FILETIME和SYSTEMTIME)

    转载请标明出处,原文地址:http://blog.csdn.net/morewindows/article/details/8654298 欢迎关注微博:http://weibo.com/MoreWi ...

  2. python有超时的windows系统时间设置代码

    手边的笔记本用久了,cmos电池可能又没电了.每次开机时间都不对,导致访问一些有https的网页会出现警告信息. 于是找了找通过python脚本设置系统时间的方法,发现了两种,其一是调用socket直 ...

  3. [2014.5.22][UBUNTU]Ubuntu与Windows系统时间不同步的问题

    安装Ubuntu+Windows双系统时会遇到Windows和Ubuntu系统时间不同步的问题,这是由于Windows系统默认读取主板bios等硬件系统时间作为OS的当地时间;而MAc,Linux类的 ...

  4. 批量自动修改windows系统时间

    windows下测试时,也许你的系统有一个功能,需要将服务器时间改到未来的某一天,但由于每一天可能都有定时的任务要走,所以直接改到未来某一天,可能系统或数据会不正常,需要一天一天改直到那一天. 如果人 ...

  5. Windows系统下Oracle每天自动备份

    linux和unix下面使用shell可以很方便实现,如果windows环境下可以结合计划任务实现 创建备份目录d:\backup, 创建批处理命令Bak.bat,编写备份脚本 exp user/pa ...

  6. Windows系统下使用Jenkins 自动发布 .NET core到Linux平台下Docker

    准备工作(安装过程可以百度,已安装的可以跳过) a)     安装Jenkins,安装包下载地址:http://mirrors.tuna.tsinghua.edu.cn/jenkins/windows ...

  7. windows系统时间(SYSTEMTIME)

    GetSystemTime函数获得当前的UTC时间,GetLocalTime获得当前的本地时间 UTC是协调世界时(Universal Time Coordinated)英文缩写,是由国际无线电咨询委 ...

  8. CentOS7 使用ntp设置系统时间,开机自动设置时间,

    首先如果没有安装ntp自己装一下: yum install -y ntp 然后,如果开了防火墙,记得打开自己的123端口,该端口是ntp用来同步时间的 firewall-cmd --zone=publ ...

  9. bat 同步windows系统时间

    需要使用管理员权限运行 net start w32timew32tm /config /updatew32tm /resync /rediscovernet stop w32timepause

随机推荐

  1. Photoshop画笔工具的使用

    现在我们按下[B]从工具栏选择画笔工具,如果选中了铅笔就[SHIFT B]切换到画笔.然后按下[D],它的作用是将颜色设置为默认的前景黑色.背景白色.也可以点击工具栏颜色区的默认按钮(下左图红色箭头处 ...

  2. 装饰器模式-Decerator

    一.定义 装饰器模式又叫做包装模式(Wrapper).装饰器模式以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 在以下情况下应该使用装饰器模式: 1.需要扩展一个类的功能,或给一个类增 ...

  3. PDO扩展

    <?php class db extends PDO { private $error; private $sql; private $bind; private $errorCallbackF ...

  4. 函数柯里化(Currying)小实践

    什么是函数柯里化 在计算机科学中,柯里化(Currying)是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数且返回结果的新函数的技术.这个技术由 Ch ...

  5. java 关键字volatile

    一.Java内存模型 想要理解volatile为什么能确保可见性,就要先理解Java中的内存模型是什么样的. Java内存模型规定了所有的变量都存储在主内存中.每条线程中还有自己的工作内存,线程的工作 ...

  6. 冒泡排序算法-python

    冒泡排序:每两个相互比较,总是选出大的相互交换,直至最后选出该列表中最大的数字 def bubbleSort(myList): for i in range(len(myList)-1):#一共进行几 ...

  7. idea2019.2 svn 忽略文件问题

    自己用的是idea2019.2最新版本,今天提交的时候Commit Changes Dialog local changes refresh一直再刷新 其他的方法都是老版本都不适合 解决办法 找到Se ...

  8. List of yellow pages

    List of yellow pages From Wikipedia, the free encyclopedia   [hide]This article has multiple issues. ...

  9. day19—纯CSS实现菜单列表下框跟随效果

    转行学开发,代码100天——2018-04-04 今天看到一篇介绍利用CSS实现列表下跟随效果的设计文章,如下图,当鼠标滑过列表项时,要求该项内容下的黑色下边框线实现同方向的跟随移动. 其中,列表内容 ...

  10. Jenkins使用一:CentOS7安装Jenkins

    安装jdk环境: yum search jdk 装 1.8版本的:yum install -y java-1.8.0-openjdk 安装Jenkins wget -O /etc/yum.repos. ...