tl;博士
OffsetDateTime.parse( "2016-01-28T12:08:47.676706-05:00" )
java.time
使用现代的 java.time 类来代替麻烦的旧日期时间类。
传统类的分辨率仅限于毫秒,而 java.time 类的分辨率要精细得多,为纳秒。这对于您输入字符串中的微秒来说已经绰绰有余了。
您的输入符合标准ISO 8601 格式。 java.time 类在解析/生成字符串时默认使用标准格式。所以根本不需要指定格式模式。
我们将您的输入解析为 OffsetDateTime,因为它包含与 UTC 的偏移量,但不包含时区。
OffsetDateTime odt = OffsetDateTime.parse( "2016-01-28T12:08:47.676706-05:00" ) ;
odt.toString(): 2016-01-28T12:08:47.676706-05:00
要查看与 UTC 值相同的时刻,请提取 Instant。
Instant instant = odt.toInstant() ;
instant.toString(): 2016-01-28T17:08:47.676706Z
或者调整到时区,得到ZonedDateTime对象。
以continent/region 的格式指定proper time zone name,例如America/Montreal、Africa/Casablanca 或Pacific/Auckland。切勿使用 3-4 个字母的缩写,例如 EST 或 IST,因为它们不是真正的时区,没有标准化,甚至不是唯一的 (!)。
ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;
zdt.toString(): 2016-01-29T06:08:47.676706+13:00[太平洋/奥克兰]
要生成其他格式的字符串,请在 Stack Overflow 中搜索 DateTimeFormatter 类。已经讲过很多次了。
关于java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.Date、Calendar 和 SimpleDateFormat。
Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。
要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310。
从哪里获得 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如Interval、YearWeek、YearQuarter 和more。