IAm*_*aja 2 java datetime jodatime
我正在尝试使用JODA将数字时间戳(long代表Unix纪元时间)转换为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);
Run Code Online (Sandbox Code Playgroud)
输出到1970年1月16日 !!!
这对任何人都有意义吗?!?!
在long您传递到DateTime构造函数,就是要在毫秒,而不是秒 -所以使用1315600867000L来代替,而这一切都很好.
文件说明:
使用ISOChronology在默认时区中构造一个设置为1970-01-01T00:00:00Z的毫秒的实例.
如果你得到一个已经在几秒钟内的值,你只需要乘以1000:
long timestampInSeconds = getValueFromDatabase();
long timestampInMillis = timestampInSeconds * 1000L;
DateTime dt = new DateTime(timestampInMillis);
Run Code Online (Sandbox Code Playgroud)
我实际上建议你Instant在这种情况下使用而不是DateTime- 你真的没有时区可以考虑.如果你正在打算使用DateTime,应明确指定的时间段,如
DateTime dt = new DateTime(timestampInMillis, DateTimeZone.UTC);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
637 次 |
| 最近记录: |