String strDateTime = "2015-08-14 16:25:12"; //From database that was stored in UTC
DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss").withZone(DateTimeZone.UTC);
DateTime dateTime = DateTime.parse(strDateTime, dateTimeFormatter);
LocalDateTime local = dateTime.toLocalDateTime();
// local = 2015-08-14T16:25:12.000
I want to get the date in my local time zone???
where local = 2015-08-14T11:25:12.000 // 11 VS 16
Run Code Online (Sandbox Code Playgroud)
知道我在这里做错了什么吗?
小智 6
使用 ISO 日期时间格式试试这个:
String startDate = "2013-07-12T18:31:01.000Z";
DateTime dt = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC).parseDateTime(startDate);
Run Code Online (Sandbox Code Playgroud)
在这里您有更多信息:http : //joda-time.sourceforge.net/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime%28%29
在 Joda 中, aLocalDateTime不是您现在所在的DateTime,它是一个没有任何时区信息的 DateTime。从文档:
LocalDateTime 是一个不可修改的日期时间类,表示没有时区的日期时间。
通过要求 dateTime.toLocalDateTime(),您只是要求与原始 UTC 版本相同的 DateTime,但 UTC 信息已被剥离。相反,听起来您想在时区之间进行转换;LocalDateTime 可能对您或您的解决方案没有用。
使用DateTime.toDateTime(DateTimeZone)在时区之间进行转换;您可以使用DateTimeZone.getDefault来获取系统的默认时区,这可能是您正在寻找的。
| 归档时间: |
|
| 查看次数: |
2684 次 |
| 最近记录: |