System.currentTimeMillis()

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

猜你喜欢

转载自blog.csdn.net/weixin_40803257/article/details/84858013