必威体育Betway必威体育官网
当前位置:首页 > IT技术

System.currentTimeMillis()计算方式与时间的单位转换

时间:2019-11-06 08:13:25来源:IT技术作者:seo实验室小编阅读:90次「手机版」
 

currenttimemillis

一、时间的单位转换

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(),通常性能就是这样一点一点地消耗掉,这里其实可以声明一个引用。

//获得系统的时间,单位为毫秒,转换为妙
long totalMilliSeconds = System.currentTimeMillis();
long totalSeconds = totalMilliSeconds / 1000;

//求出现在的秒
long currentSecond = totalSeconds % 60;

//求出现在的分
long totalMinutes = totalSeconds / 60;
long currentMinute = totalMinutes % 60;

//求出现在的小时
long totalHour = totalMinutes / 60;
long currentHour = totalHour % 24;

//显示时间
System.out.println("总毫秒为: " + totalMilliSeconds);
System.out.println(currentHour + ":" + currentMinute + ":" + currentSecond + " GMT");

小例子

package demo.spli;

import java.text.DateFormat;
import java.text.simpledateformat;
import java.util.Date;
import java.util.TimeZone;


public class ShowCurrentTime {

    /**
     * @显示当前时间
     * @2014.9.3
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //获得系统的时间,单位为毫秒,转换为妙
        long totalMilliSeconds = System.currentTimeMillis();
        
         DateFormat dateformatterChina = DateFormat.getDateTimeinstance(DateFormat.medium,DateFormat.MEDIUM);//格式化输出
          TimeZone timeZoneChina = TimeZone.getTimeZone("Asia/Shanghai");//获取时区 这句加上,很关键。
          dateFormatterChina.setTimeZone(timeZoneChina);//设置系统时区
        long totalSeconds = totalMilliSeconds / 1000;
        
        //求出现在的秒
        long currentSecond = totalSeconds % 60;
        
        //求出现在的分
        long totalMinutes = totalSeconds / 60;
        long currentMinute = totalMinutes % 60;
        
        //求出现在的小时
        long totalHour = totalMinutes / 60;
        long currentHour = totalHour % 24;
        
        //显示时间
        System.out.println("总毫秒为: " + totalMilliSeconds);
        System.out.println(currentHour + ":" + currentMinute + ":" + currentSecond + " GMT");
        
        
        Date nowTime = new Date(System.currentTimeMillis());
        System.out.println(System.currentTimeMillis());
          SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
          String retStrFormatNowDate = sdFormatter.format(nowTime);
          
          System.out.println(retStrFormatNowDate);
    }

}

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

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

转自:https://www.cnblogs.com/rinack/p/6776100.html

文章最后发布于: 2018-10-12 16:47:43

相关阅读

excel表格如何使用时间函数

在excel表格中,经常会用到各种函数,那么时间函数具体的使用步骤是怎样的呢?下面就跟seo实验室小编一起来看看吧。excel表格使用时间

闲鱼多久自动确认收货?卖家什么时间内理会买家退货申

在闲鱼上面购买二手商品,大家知道闲鱼多久自动确认收货吗?如果在中途不想要商品了,那么卖家什么时间内理会买家退货申请?对于这两个

DNF10周年活动时间预测 DNF国服10周年活动什么时候开

dnf国服10周年活动什么时候开始?dnf即将迎来dnf国服十周年,一般活动都会在提前发喔,接下来一起来了解下dnf十周年活动预测吧!dnf国

ipad pro2018发布时间确定:iOS 12.1透露新设备相关消息

A5创业网(公众号:iadmin5)9月19日报道,今天凌晨,iOS 12.1 首个开发者测试版更新已经发布。根据开发者透露的消息,iOS 12.1为Face ID内建

关于《超越时间线》

这两天看了一部美剧,《超越时间线》,在verycd上的评分还挺高,也还算是好看。穿越这个题材越来越被广泛采用了,而且是不分国界,区别就在

分享到:

栏目导航

推荐阅读

热门阅读