当前位置: 代码迷 >> J2SE >> 毫秒数转换成时分秒格式,该怎么解决
  详细解决方案

毫秒数转换成时分秒格式,该怎么解决

热度:129   发布时间:2016-04-24 17:31:45.0
毫秒数转换成时分秒格式
java中有什么方法可以把一个毫秒数格式化成 "时:分:秒 "形式的?
比如我想把4808000毫秒格式化成 "01:20:08 "


------解决方案--------------------
Date date = new Date(time);
System.out.println(date);
------解决方案--------------------
java.util.Date;
------解决方案--------------------
。。。。。问住了。。。。
除了手写的方法外还有能在JDK中找到的么?
------解决方案--------------------
import java.util.*;
import java.text.DateFormat;
public class Test{
public static void main(String[] args)
{
long hour = 4808000/(60*60*1000);
long minute = (4808000 - hour*60*60*1000)/(60*1000);
long second = (4808000 - hour*60*60*1000 - minute*60*1000)/1000;
if(second > = 60 )
{
second = second % 60;
minute+=second/60;
}
if(minute > = 60)
{
minute = minute %60;
hour += minute/60;
}
String sh = " ";
String sm = " ";
String ss = " ";
if(hour <10)
{
sh = "0 " + String.valueOf(hour);
}else
{
sh = String.valueOf(hour);
}
if(minute <10)
{
sm = "0 " + String.valueOf(minute);
}else
{
sm = String.valueOf(minute);
}
if(second <10)
{
ss = "0 " + String.valueOf(second);
}else
{
ss = String.valueOf(second);
}
System.out.println(sh + sm + ss);
System.out.println(hour+ "a " + minute + "a " + second + "a ");
}


}
------解决方案--------------------
long time = 123456789;
Date date = new Date(time);
System.out.println(date.getHours() + ": " + date.getMinutes() + ": " + date.getSeconds());
------解决方案--------------------
Date date = new Date(time);
SimpleDateFormat aFormat = new SimpleDateFormat( "HH:mm:ss ");
System.out.println(aFormat.format(date));
------解决方案--------------------
public static void main(String[] args)
{
long timeCount=4808000;
Date date=new Date(4808000);

SimpleDateFormat sf=new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss ");
System.out.println(sf.format(date));
}

结果是:1970-01-01 09:20:08
不知道楼主对结果满意不??
  相关解决方案