System.currentTimeMillis()如何转换为此刻的日期及时间呢?
下文笔者讲述将当前的毫秒数转换为此刻的日期及时间的方法分享
毫秒数转换为此刻的日期及时间的实现思路
将毫秒数转换为秒数及分钟数,小时数字 如: //获得系统的时间,单位为毫秒,转换为妙 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); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。