System.currentTimeMillis()如何转换为此刻的日期及时间呢?

戚薇 Java经验 发布时间:2023-05-17 21:28:13 阅读数:7770 1
下文笔者讲述将当前的毫秒数转换为此刻的日期及时间的方法分享

毫秒数转换为此刻的日期及时间的实现思路

将毫秒数转换为秒数及分钟数,小时数字

如:
//获得系统的时间,单位为毫秒,转换为妙
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 com.java265;

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


public class TestClass {

    /**
     * @显示当前时间
     * @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 d = sdFormatter.format(nowTime);
          
        System.out.println(d);
    }
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202305/16843301606507.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者