【发布时间】:2014-06-29 01:51:51
【问题描述】:
我最近迁移到 Java 8,希望能更轻松地处理本地和分区时间。
但是,在我看来,在解析简单日期时,我遇到了一个简单的问题。
public static ZonedDateTime convertirAFecha(String fecha) throws Exception {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
ConstantesFechas.FORMATO_DIA).withZone(
obtenerZonaHorariaServidor());
ZonedDateTime resultado = ZonedDateTime.parse(fecha, formatter);
return resultado;
}
就我而言:
- fecha 是 '15/06/2014'
- ConstantesFechas.FORMATO_DIA 是 'dd/MM/yyyy'
- obtenerZonaHorariaServidor 返回 ZoneId.systemDefault()
所以,这是一个简单的例子。但是,解析会抛出此异常:
java.time.format.DateTimeParseException: 文本 '15/06/2014' 不能 被解析:无法从 TemporalAccessor 获取 ZonedDateTime: {},ISO 解析为 java.time.format.Parsed 类型的 2014-06-15
有什么建议吗?我一直在尝试解析和使用 TemporalAccesor 的不同组合,但到目前为止没有任何运气。
【问题讨论】:
-
这些确实是 fechas :) !
-
我在这里详细讨论了
java.time背后的理论:stackoverflow.com/a/56508200/145989
标签: java timezone java-8 datetime-format java-time