如何解析 UTC 时间并在 JodaTime java 中显示本地时间?
How do I parse UTC time and display the local time in JodaTime java?
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
知道我做错了什么吗?
尝试使用 ISO 日期时间格式:
String startDate = "2013-07-12T18:31:01.000Z";
DateTime dt = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC).parseDateTime(startDate);
这里有更多信息:http://joda-time.sourceforge.net/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime%28%29
在 Joda 中,LocalDateTime
不是您现在所在的 DateTime,它是 没有任何时区信息的 DateTime.来自 the docs:
LocalDateTime is an unmodifiable datetime class representing a datetime without a time zone.
通过请求 dateTime.toLocalDateTime(),您只是请求与原始 UTC 版本相同的 DateTime,但 UTC 信息被删除。相反,听起来您想在时区之间进行转换; LocalDateTime 可能对您或您的解决方案没有用。
使用DateTime.toDateTime(DateTimeZone) to convert between time zones; you can use DateTimeZone.getDefault获取系统的默认时区,这可能就是您要找的。
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
知道我做错了什么吗?
尝试使用 ISO 日期时间格式:
String startDate = "2013-07-12T18:31:01.000Z";
DateTime dt = ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC).parseDateTime(startDate);
这里有更多信息:http://joda-time.sourceforge.net/apidocs/org/joda/time/format/ISODateTimeFormat.html#dateTime%28%29
在 Joda 中,LocalDateTime
不是您现在所在的 DateTime,它是 没有任何时区信息的 DateTime.来自 the docs:
LocalDateTime is an unmodifiable datetime class representing a datetime without a time zone.
通过请求 dateTime.toLocalDateTime(),您只是请求与原始 UTC 版本相同的 DateTime,但 UTC 信息被删除。相反,听起来您想在时区之间进行转换; LocalDateTime 可能对您或您的解决方案没有用。
使用DateTime.toDateTime(DateTimeZone) to convert between time zones; you can use DateTimeZone.getDefault获取系统的默认时区,这可能就是您要找的。