欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java System.currentTimeMillis()时间的单位转换与计算方式案例详解

时间:2021-11-12 09:16:17|栏目:JAVA代码|点击:

一、时间的单位转换

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,则输出:当前时间的一分钟后

上一篇:Java实现Redis延时消息队列

栏    目:JAVA代码

下一篇:mybatis如何返回某列的最大值

本文标题:Java System.currentTimeMillis()时间的单位转换与计算方式案例详解

本文地址:http://www.codeinn.net/misctech/183245.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有