相关疑难解决方法(0)

无法使用Java 8中的DateTimeFormatter和ZonedDateTime从TemporalAccessor获取ZonedDateTime

我最近搬到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;
}
Run Code Online (Sandbox Code Playgroud)

就我而言:

  • fecha是'15/06/2014'
  • ConstantesFechas.FORMATO_DIA是'dd/MM/yyyy'
  • obtenerZonaHorariaServidor返回ZoneId.systemDefault()

所以,这是一个简单的例子.但是,解析会抛出此异常:

java.time.format.DateTimeParseException: Text '15/06/2014' could not be parsed: Unable to obtain ZonedDateTime from TemporalAccessor: {},ISO resolved to 2014-06-15 of type java.time.format.Parsed
Run Code Online (Sandbox Code Playgroud)

有小费吗?我一直在尝试解析和使用TemporalAccesor的不同组合,但到目前为止没有任何运气.

最好的祝福

java timezone datetime-format java-8 java-time

36
推荐指数
4
解决办法
3万
查看次数

标签 统计

datetime-format ×1

java ×1

java-8 ×1

java-time ×1

timezone ×1