关于获取系统时间的差异

几种获取系统时间的异同

System类时JDK中的API;SystemClock是Android中的API,跟设备从启动开始获取时间有关。

1.System.currentTimeMillis();表示1970年0点0时0分0秒距离现在的毫秒数。

System#currentTimeMillis

Returns the current time in milliseconds since January 1, 1970 00:00:00.0 UTC.

2.SystemClock.uptimeMillis();表示系统从启动到现在的毫秒数(不包括深度睡眠的时间)。

android.os.SystemClock#uptimeMillis

Returns milliseconds since boot, not counting time spent in deep sleep.

3.SystemClock.elapsedRealtime();表示系统从启动到现在的毫秒数(包括深度睡眠的时间)。

android.os.SystemClock#elapsedRealtime

Returns milliseconds since boot, including time spent in sleep

4.SystemClock.elapsedRealtimeNanos();现在还不知道具体的意义,不知道和SystemClock.elapsedRealtime()的区别。

android.os.SystemClock#elapsedRealtime

Returns nanoseconds since boot, including time spent in sleep.

关于用法:

1.System.currentTimeMillis();可以用模板设计模式来设计一个计算一个方法执行时间的模板

/**
  • 本来采用模板设计模式,来设计出一个计算某个方法运行的事件
  • @author Administrator
    */
    public abstract class Systemtime_template {
    public long getRunMethodTime() {
    long start = System.currentTimeMillis();
    method();
    long end = System.currentTimeMillis();
    return end - start;
    }
    public abstract void method();
    }

关于System类

1.System类中的很多方法都和系统底层、虚拟机有关。比如说在源代码中currentTimeMillis();registerNatives();mapLibraryName();setIn0()(输入流System.in底层转调);setOut0(System.out底层转调);setErr0();都是本地方法,具体实现是由c代码完成的。

2.Runtime类初始化是单例形式Runtime.getRuntime();System.gc()方法(垃圾回收)其实是转调Runtime.getRuntime().gc()的,而Runtime中的gc()方法其实也是本地方法,底层是用c代码实现的。public native void gc();

3.在写jni的时候就是通过System.loadLibrary()方法来加载库文件的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值