我正在尝试使用JODA将数字时间戳(表示Unix纪元时间的long
)转换为Month Day, Year
字符串。
这是我几秒钟前刚刚运行的代码:
long lTimestamp = 1315600867; // Current timestamp is approx 9/9/11 3:41 PM EST
DateTime oTimestamp = new DateTime(lTimestamp);
String strMon, strDay, strYear;
strMon = oTimestamp.monthOfYear().getAsText(Locale.ENGLISH);
strDay = oTimestamp.dayOfMonth().getAsText(Locale.ENGLISH);
strYear = oTimestamp.year().getAsText(Locale.ENGLISH);
String strDate = strMon + " " + strDay + ", " + strYear;
System.out.println("Converted timestamp is : " + strDate);
输出到 1970年1月16日 !!!
这对任何人 任何有意义吗?!?!
答案 0 :(得分:8)
您传入DateTime
constructor的long
意味着毫秒,而不是秒 - 所以请改用1315600867000L,这一切都很好
文档说明:
使用ISOChronology在默认时区中构造一个设置为1970-01-01T00:00:00Z的毫秒的实例。
如果您获得的值已经是几秒钟,则只需要乘以1000:
long timestampInSeconds = getValueFromDatabase();
long timestampInMillis = timestampInSeconds * 1000L;
DateTime dt = new DateTime(timestampInMillis);
我实际上建议你在这种情况下使用Instant
而不是DateTime
- 你真的没有时区可以考虑。如果 要使用DateTime
,则应明确指定时区,例如
DateTime dt = new DateTime(timestampInMillis, DateTimeZone.UTC);