2013-04-09 81 views
2
long mil = new Date().getTime(); 
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(mil); 
System.out.println(formatter.format(calendar.getTime())); 

上面的代码返回1970年1月2日21时57分09秒。问题是什么?转换毫秒为日期1970年收益为一年

编辑: 我道歉。它不适用于android。 (Android问题)。

+0

问题出在哪里?代码完成你所要做的事情。 – 2013-04-09 12:21:14

+4

检查您的系统时间。 – eatSleepCode 2013-04-09 12:21:49

+3

你可以添加一行来打印'mil'来查看未格式化的系统时间。 – pauli 2013-04-09 12:23:48

回答

0

你为什么这样做..不能ü只是做到这一点?:

Date d = new Date(); 
SimpleDateFormat..... 
System.out.println(formatter.format(d)); 
0

你想做的事可以用一个简单的方法来实现的事情:

Date currentDate = System.currentTimeMillis(); 
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
System.out.println("Formatted date time::: " +formatter.format(currentDate)); 

希望它可以帮助你。

0

仅供参考,您可以使用Android上的乔达时库。代码更简单,更清晰。

System.out.println(DateTime.now()); 

当运行...

2014-02-03T04:05:06.789Z