且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-02-08 18:28:16

我不确定为什么它不起作用(可能是因为你的输入没有时间/时区信息)。一种简单的方法是首先将您的日期解析为 LocalDate (没有时区或时区信息)然后创建 ZonedDateTime

I am not sure why it does not work (probably because your input does not have time/time zone information). A simple way is to parse your date as a LocalDate first (without time or time zone information) then create a ZonedDateTime:

public static ZonedDateTime convertirAFecha(String fecha) {
  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
  LocalDate date = LocalDate.parse(fecha, formatter);

  ZonedDateTime resultado = date.atStartOfDay(ZoneId.systemDefault());
  return resultado;
}