将毫秒转换为日期将1970年作为年份返回

时间:2013-04-09 12:19:45

标签: java android date calendar

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-01-02 21:57:09 。有什么问题?

它无法在Android中运行。

3 个答案:

答案 0 :(得分:0)

你为什么这样做?..你不能简单地这样做吗?:

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

答案 1 :(得分:0)

您想要做的事情可以通过一种简单的方法来实现:

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

希望它对你有所帮助。

答案 2 :(得分:0)

仅供参考,您可以在Android上使用Joda-Time库。代码更简单,更清晰。

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

跑步时......

2014-02-03T04:05:06.789Z