SimpleDateFormat格式错误的值 [英] SimpleDateFormat format wrong values

查看:90
本文介绍了SimpleDateFormat格式错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码:

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");
System.out.println(sdf.format(new Date(1293253200))); // 12/25/2010 05:00 GMT
System.out.println(sdf.format(new Date(1293339600))); // 12/26/2010 05:00 GMT
System.out.println(sdf.format(new Date(1293426000))); // 12/27/2010 05:00 GMT

打印:

01/16
01/16
01/16

通过 SimpleDateFormat.getDateInstance();使用默认的 DateFormat 将这些日期打印为 1970年1月16日.发生了什么事?

Using a default DateFormat via SimpleDateFormat.getDateInstance(); prints these dates as 16-Jan-1970. What is going on?

推荐答案

您正在混合毫秒和秒.1293253200确实是2010年1月16日.您必须乘以1000才能获得所需的日期:

You are mixing milliseconds and seconds. 1293253200 is indeed 16. January 2010. You have to multiply with 1000 to get the dates you wanted:

Date date = new Date(1293253200L*1000L);
Sat Dec 25 06:00:00 CET 2010

这篇关于SimpleDateFormat格式错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆