System类代表系统,系统级的很多属性和控制方法都放置在该类的内部。该类位于java.lang包。

平时产生随机数时我们经常拿时间做种子,比如用System.currentTimeMillis的结果,但是在执行一些循环中使用了System.currentTimeMillis,那么每次的结果将会差别很小,甚至一样,因为现代的计算机运行速度很快。后来看到java中产生随机数函数以及线程池中的一些函数使用的都是System.nanoTime,下面说一下这2个方法的具体区别。

 System.nanoTime

System.nanoTime提供相对精确的计时,但是不能用他来计算当前日期,在jdk中的说明如下:

public static long nanoTime() 返回最准确的可用系统计时器的当前值,以毫微秒为单位。

此方法只能用于测量已过的时间,与系统或钟表时间的其他任何时间概念无关。返回值表示从某一固定但任意的时间算起的毫微秒数(或许从以后算起,所以该值可能为负)。此方法提供毫微秒的精度,但不是必要的毫微秒的准确度。它对于值的更改频率没有作出保证。在取值范围大于约 292 年(263 毫微秒)的连续调用的不同点在于:由于数字溢出,将无法准确计算已过的时间。

例如,测试某些代码执行的时间长度:

long startTime = System.nanoTime();

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

long estimatedTime = System.nanoTime() - startTime;

返回:系统计时器的当前值,以毫微秒为单位。从以下版本开始:1.5

详细代码:

public class Test3 {
    public static void main(String[] args) {
        String[] a = new String[] { "aaa", "aaa", "ccc" };
        String[] b = new String[] { "aaa", "bbb", "ccc" };
        Clock clock = Clock.getClock();
        long start = System.nanoTime(); // 获取开始时间
        System.out.println(equals(a,b));
        long end = System.nanoTime(); // 获取结束时间
        clock.stopAndPrint("耗时:");
        System.out.println(end + "===" + start);
    }
    //比较两个string[] 无序是否相等
    public static boolean equals(String a[],String b[]){
           if(a.length!=b.length) return false;
           ].hashCode()^b[].hashCode();
           ;i<a.length;i++){
                n^=a[i].hashCode()^b[i].hashCode();
          }
           ) return true;
             return false;
        }
}  

java。lang包

/**
     * Returns the current value of the most precise available system
     * timer, in nanoseconds.
     *
     * <p>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 time (perhaps in the future, so values may be
     * negative).  This method provides nanosecond precision, but not
     * necessarily nanosecond accuracy. No guarantees are made about
     * how frequently values change. Differences in successive calls
     * that span greater than approximately 292 years (2<sup>63</sup>
     * nanoseconds) will not accurately compute elapsed time due to
     * numerical overflow.
     *
     * <p> For example, to measure how long some code takes to execute:
     * <pre>
     *   long startTime = System.nanoTime();
     *   // ... the code being measured ...
     *   long estimatedTime = System.nanoTime() - startTime;
     * </pre>
     *
     * @return The current value of the system timer, in nanoseconds.
     * @since 1.5
     */

System.currentTimeMillis

System.currentTimeMillis返回的是从1970.1.1 UTC 零点开始到现在的时间,精确到毫秒,平时我们可以根据System.currentTimeMillis来计算当前日期,星期几等,可以方便的与Date进行转换,下面时jdk中的介绍:

public static long currentTimeMillis() 返回以毫秒为单位的当前时间。注意,当返回值的时间单位是毫秒时,值的粒度取决于底层操作系统,并且粒度可能更大。例如,许多操作系统以几十毫秒为单位测量时间。

请参阅 Date 类的描述,了解可能发生在“计算机时间”和协调世界时(UTC)之间的细微差异的讨论。

返回:当前时间与协调世界时 1970 年 1 月 1 日午夜之间的时间差(以毫秒为单位测量)。

所以在使用中,我们可以根据我们具体的目的去正确的选择他们。

currentTimeMillis方法

public static long currentTimeMillis()

该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数。

可以直接把这个方法强制转换成date类型。

代码如下:

long currentTime = System.currentTimeMillis();

SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒");

Date date = new Date(currentTime);

System.out.println(formatter.format(date));
运行结果如下:
当前时间:2011年-08月10日-14时11分46秒
另:
可获得当前的系统和用户属性:
       String osName = System.getProperty(“os.name”);
  String user = System.getProperty(“user.name”);
  System.out.println(“当前操作系统是:” + osName);
  System.out.println(“当前用户是:” + user);
       System.getProperty 这个方法可以得到很多系统的属性。

java.lang包:

 /**
     * 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
     */

一、时间的单位转换

1秒=1000毫秒(ms) 1毫秒=1/1,000秒(s)
1秒=1,000,000 微秒(μs) 1微秒=1/1,000,000秒(s)
1秒=1,000,000,000 纳秒(ns) 1纳秒=1/1,000,000,000秒(s)
1秒=1,000,000,000,000 皮秒(ps) 1皮秒=1/1,000,000,000,000秒(s)

1分钟=60秒

1小时=60分钟=3600秒

二、System.currentTimeMillis()计算方式

在开发过程中,通常很多人都习惯使用new Date()来获取当前时间。new Date()所做的事情其实就是调用了System.currentTimeMillis()。如果仅仅是需要或者毫秒数,那么完全可以使用System.currentTimeMillis()去代替new Date(),效率上会高一点。如果需要在同一个方法里面多次使用new Date(),通常性能就是这样一点一点地消耗掉,这里其实可以声明一个引用。

可以看出输出的时间是当前时间的一个小时后。

System.currentTimeMillis()+3600*1000)可以这样解读:System.currentTimeMillis()相当于是毫秒为单位,但是,后头成了1000,就变成了以秒为单位。那么,3600秒=1小时,所以输出为当前时间的1小时后。

我们可以这样控制时间:System.currentTimeMillis()+time*1000),里面传入的time是以秒为单位,当传入60,则输出:当前时间的一分钟后

可以看出输出的时间是当前时间的一分钟后。

有不足之处,敬请谅解!!

System.nanoTime与System.currentTimeMillis的理解与区别的更多相关文章

  1. System.nanoTime与System.currentTimeMillis的区别

    平时产生随机数时我们经常拿时间做种子,比如用 System.currentTimeMillis的结果,但是在执行一些循环中使用了System.currentTimeMillis,那么每次的结 果将会差 ...

  2. System.nanoTime()和System.currentTimeMillis()性能问题

    ​ 之前给模块做性能优化的时候,需要将性能调到毫秒级,使用了System.nanoTime()和System.currentTimeMillis()对代码分片计时分析耗时操作,后发现在串行情况下性能达 ...

  3. System.nanoTime与System.currentTimeMillis比较

    System.nanoTime与System.currentTimeMillis比较 ​currentTimeMillis返回的是系统当前时间和1970-01-01之前间隔时间的毫秒数,如果系统时间固 ...

  4. 我的Java开发学习之旅------>System.nanoTime与System.currentTimeMillis的区别

    首先来看一道题:下面代码的输出结果是什么? import java.util.HashMap; import java.util.Map; public class HashMapTest { pub ...

  5. System.nanoTime与System.currentTimeMillis的区别(转)

    原文地址:http://blog.csdn.net/dliyuedong/article/details/8806868 平时产生随机数时我们经常拿时间做种子,比如用System.currentTim ...

  6. System.nanoTime与System.currentTimeMillis

    System.nanoTime提供相对精确的计时,但是不能用他来计算当前日期.(系统计时器的当前值,以毫微秒为单位) System.currentTimeMillis返回的是从1970.1.1 UTC ...

  7. java: new Date().getTime() 与 System.currentTimeMillis() 与 System.nanoTime()

    java使用new Date()和System.currentTimeMillis()获取当前时间戳   在开发过程中,通常很多人都习惯使用new Date()来获取当前时间,使用起来也比较方便,同时 ...

  8. java的System.currentTimeMillis()和System.nanoTime

    纳秒 ns(nanosecond):纳秒, 时间单位.一秒的10亿分之一,即等于10的负9次方秒.常用作 内存读写速度的单位,其前面数字越小则表示速度越快.   1纳秒=1000 皮秒   1纳秒 = ...

  9. System.currentTimeMillis和System.nanoTime()

    ns(nanosecond):纳秒, 时间单位.一秒的10亿分之一,即等于10的负9次方秒.常用作 内存读写速度的单位.  1纳秒=0.000001 毫秒  1纳秒=0.00000 0001秒 jav ...

随机推荐

  1. NPM如何更新到最新版

    参考文章--npm更新到最新版本的方法 其实我们可以这样,随便新建一个文件夹例如:F:\test.按着"shift"键,右键该文件夹,选择"在此处打开命令窗口(W)&qu ...

  2. [转载]敏捷开发之Scrum扫盲篇

    现在敏捷开发是越来越火了,人人都在谈敏捷,人人都在学习Scrum和XP...      为了不落后他人,于是我也开始学习Scrum,今天主要是对我最近阅读的相关资料,根据自己的理解,用自己的话来讲述S ...

  3. svnserver hook python

    在使用中可能会遇到的错误排除 :1.Error: svn: 解析"D:\www\test"出错,或svn: E020024: Error resolving case of 'D: ...

  4. 在jexus下如何简单的配置多站点

    参考:linuxdot.net 其实jexus的配置还是比较简单的,目录即站点(一个目录就是一个站点,一个配置文件就是一个站点) 如uustudy.net,在siteconf目录下创建一个uustud ...

  5. 一步步开发自己的博客 .NET版(1、基本显示)

    前言 我们每个猿都有一个搭建自己独立博客的梦,我也不例外.以前想 现在想 以后也想.之所以一直迟迟没有着手,是因为难以跨出第一步.每次心里想着,等我以后技术好了再说,然后就没有然后了.以前用过word ...

  6. 跟Unity3D学代码优化

    今天我们来聊聊如何跟Unity学代码优化,准确地说,是通过学习Unity的IL2CPP技术的优化策略,应用到我们的日常逻辑开发中. 做过Unity开发的同学想必对IL2CPP都很清楚,简单地说,IL2 ...

  7. ABP源码分析三:ABP Module

    Abp是一种基于模块化设计的思想构建的.开发人员可以将自定义的功能以模块(module)的形式集成到ABP中.具体的功能都可以设计成一个单独的Module.Abp底层框架提供便捷的方法集成每个Modu ...

  8. form表单 ----在路上(15)

    form 表单就是将用户的信息提交到服务器,服务器会将信息存储活着根据信息查询数据进行增删改查,再将其返回给用户. 基本格式: <form action="" method ...

  9. [原创]MYSQL中利用外键实现级联删除和更新

    MySQL中利用外键实现级联删除.更新 MySQL支持外键的存储引擎只有InnoDB,在创建外键的时候,要求父表必须有对应的索引,子表在创建外键的时候也会自动创建对应的索引.在创建索引的时候,可以指定 ...

  10. 高性能JavaScript--快速响应的用户界面(简要学习笔记三)

    1.浏览器线程:用于执行JavaScript和更新用户界面的进程被称为“浏览器UI线程”.   2. <1>定时器的出现让出UI线程控制权 setTimeout(),setInterval ...