我正在使用Android和Joda Time库,试图转换用户的时区,以便稍后将其格式化为:例如2012-11-12T21:45:00+02:00。
DateTimeZone zone = DateTimeZone.forID( TimeZone.getDefault().getID());
以上的代码失败了 - 有人知道如何将 "Europe/London" (时区ID) 转换为偏移量,以便我可以将其放入 ISO 8601 格式中吗?
SimpleDateFormat
类。SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.UK);
String formattedDate = sdf.format(new Date());
API 26增加了对Java中许多时间和日期类的支持。因此,现在也可以应用此解决方案:https://dev59.com/wF8e5IYBdhLWcg3wlbBR#25618897
// works with Instant
Instant instant = Instant.now();
System.out.println(instant.format(DateTimeFormatter.ISO_INSTANT));
// works with ZonedDateTime
ZonedDateTime zdt = ZonedDateTime.now();
System.out.println(zdt.format(DateTimeFormatter.ISO_INSTANT));
// example output
2014-09-02T08:05:23.653Z
Credits to JodaStephen.
Android文档: https://developer.android.com/reference/java/time/format/DateTimeFormatter.html#ISO_INSTANT
yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
。 - OhhhThatVarun