zoukankan      html  css  js  c++  java
  • new Date().getTime()和System.currentTimeMillis()获取时间戳的比较

    最近在优化项目代码,看项目组的代码时,发现了一个有趣的现象,有使用new Date().getTime()来获取时间戳的, 也有使用System.currentTimeMillis()来获取时间戳的,这让我想到,好像我平时写代码也是想起哪种方式就用什么方式写。这两种方式都可以,仔细思考一下,两者应该会有区别的,应该有是最优的方式?

    然后就看了看源码,其实解决疑惑最优方式就是看源码,打开java.util.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());
        }
    
        /**
         * Allocates a <code>Date</code> object and initializes it to
         * represent the specified number of milliseconds since the
         * standard base time known as "the epoch", namely January 1,
         * 1970, 00:00:00 GMT.
         *
         * @param   date   the milliseconds since January 1, 1970, 00:00:00 GMT.
         * @see     java.lang.System#currentTimeMillis()
         */
        public Date(long date) {
            fastTime = date;
        }

    从源码可以看出,new Date().getTime()其实就是在无参构造里调用了System.currentTimeMillis(),再传入自己的有参构造函数。不难看出,如果只是仅仅获取时间戳,即使是匿名的new Date()对象也会有些许的性能消耗, 从性能提升的角度来看,如果只是仅仅获取时间戳,不考虑时区的影响(时区为什么会有影响),直接调用System.currentTimeMillis()会更好一些。

    再来看看System.currentTimeMillis()的源码:

        /**
         * 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();

    这是一个本地方法,其时间来源依赖由操作系统为其做了时区的处理,因此获取时间戳,不需要考虑时区的前提下,它是最优选择。

    其实, java.util.Date设计来作为格式化时间,以面向对象的方式获取与时间有关的各方面信息,例如:获取年月份、小时、分钟等等比较丰富的信息。而new Date()来获取当前时间更多的是因为我们使用习惯导致经常第一时间想到用它来获取当前时间。

    扩展:关于获取时间戳另一种方式:

    在Java中,还可能见到另外一种获取时间的方式:

    Calendar.getInstance().getTimeInMillis()

    其实这种方式是速度最慢的,看其源码就会发现,Canlendar是区分时区的,因为要处理时区问题会耗费很多的时间。

  • 相关阅读:
    MySQL数据库封装和分页查询
    程序员的价值在哪里?
    奇葩的程序员
    京东咚咚架构演进
    程序员必看的《黑客帝国》,你看懂了吗?
    微信后台技术“干货们”带来的启发
    drf框架 2 drf框架的请求生命周期(as_view和dispatch方法), 请求、解析、渲染、响应、异常, 序列化组件 ,ORM配置回顾(media文件配置),应用在settings.py中INSTALLED_APPS注册意义 ,数据库配置
    drf框架, 接口(api) Django FBV => CBV drf框架的基础试图类 drf核心组件 群查与单查 python换源
    前端Vue框架 05 第三方插件(vuex: 组件间交互的(移动端), axios
    前端Vue框架 04 路由:逻辑跳转、路由传参 项目组件的数据局部化处理data(){ return{} } 组件的生命周期钩子 组件间通信 全局配置css, js
  • 原文地址:https://www.cnblogs.com/no8g/p/13415601.html
Copyright © 2011-2022 走看看