You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法编辑日期时间且报错“Cannot format given Object as a Date”的问题咨询

解决日期编辑报错 "Cannot format given Object as a Date" 并调整显示格式

嘿,我来帮你搞定这个问题!首先咱们得先搞清楚错误的根源,然后一步步调整代码,让日期时间显示成你想要的格式,同时不再报错。

问题分析

你遇到的Cannot format given Object as a Date错误,主要是因为你的Editar方法逻辑混乱:

  1. 你先调用了dateFormatter.format(fecha),但这时候fecha还没被赋值,是空的或者不是Date类型;
  2. 之后你又直接把r.getFecha().toString()赋值给this.fecha,这就导致页面显示的是Date对象默认的toString结果(比如Sun May 27 00:00:00 COT 2018);
  3. 到了GuardarEdicion方法,你用MM/dd/yyyy格式去解析这种默认字符串,格式完全不匹配,自然就报错了。

另外,你当前显示的格式也不符合预期,这也是因为没有正确对Date对象做格式化处理。

具体修复步骤

1. 修正Editar方法的格式化逻辑

把Editar方法里的错误逻辑改掉,直接从查询到的Registros对象中获取Date类型的字段,用指定格式转为字符串后赋值给Bean属性:

public String Editar(Integer id){
    Registros r = this.registrosFacade.find(id);
    
    // 格式化日期为 MM/dd/yyyy 格式
    DateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
    this.fecha = dateFormatter.format(r.getFecha());
    
    // 格式化时间为 HH:mm:ss 格式(去掉多余的'Z'和GMT时区,匹配你想要的显示效果)
    DateFormat timeFormatter = new SimpleDateFormat("HH:mm:ss");
    this.hora_in = timeFormatter.format(r.getHoraIn());
    this.hora_out = timeFormatter.format(r.getHoraOut());
    
    this.vehiculo = r.getIdVehiculo();
    this.id = id; // 一定要保存当前编辑的ID,后续保存时需要用它查询原对象
    return "RegistroEdit";
}

2. 调整GuardarEdicion方法的解析逻辑

页面传入的是MM/dd/yyyy和HH:mm:ss格式的字符串,所以解析的时候要对应格式,同时注意不要新建Registros对象(否则会丢失其他未修改的属性),应该先查询原对象再修改:

public String GuardarEdicion() throws ParseException{
    // 先查询要编辑的原记录,避免新建对象丢失其他属性
    Registros r = this.registrosFacade.find(id);
    
    Locale locale = new Locale("es","CO");
    // 解析日期字符串为Date对象
    String datePattern = "MM/dd/yyyy";
    SimpleDateFormat dateFormatter = new SimpleDateFormat(datePattern, locale);
    Date parsedDate = dateFormatter.parse(this.fecha);
    r.setFecha(parsedDate);
    
    // 解析时间字符串为Date对象
    String timePattern = "HH:mm:ss";
    SimpleDateFormat timeFormatter = new SimpleDateFormat(timePattern, locale);
    Date horaIn = timeFormatter.parse(this.hora_in);
    r.setHoraIn(horaIn);
    
    Date horaOut = timeFormatter.parse(this.hora_out);
    r.setHoraOut(horaOut);
    
    r.setIdVehiculo(vehiculosFacade.find(vehiculo.getId()));
    this.registrosFacade.edit(r);
    return "RegistroList";
}

3. 优化页面保存按钮的参数

修改RegistroEdit.xhtml里的保存按钮,不需要传入多余的参数,直接调用Bean的方法即可:

<h:commandButton id="registroCommand" value="Guardar" action="#{registroController.GuardarEdicion()}"/>

4. (可选)用PrimeFaces组件提升用户体验

如果项目里用了PrimeFaces,推荐用专门的日期时间组件来处理,这样能自动限制输入格式,减少手动格式化的麻烦:

<!-- 日期选择组件,自动匹配MM/dd/yyyy格式 -->
<p:outputLabel value="Fecha:" for="fecha" />
<p:calendar id="fecha" value="#{registroController.fecha}" pattern="MM/dd/yyyy" />

<!-- 时间输入掩码,强制用户输入HH:mm:ss格式 -->
<p:outputLabel value="HoraIn:" for="horaIn" />
<p:inputMask id="horaIn" value="#{registroController.hora_in}" mask="99:99:99" required="true" requiredMessage="The HoraIn field is required."/>

<p:outputLabel value="HoraOut:" for="horaOut" />
<p:inputMask id="horaOut" value="#{registroController.hora_out}" mask="99:99:99" required="true" requiredMessage="The HoraOut field is required."/>

核心思路总结

整个修复的关键是保证数据库取出的Date对象 → 正确格式化为页面需要的字符串,以及页面传入的字符串 → 正确解析回Date对象存入数据库,同时要避免逻辑顺序错误(比如先格式化未初始化的变量)。

内容的提问来源于stack exchange,提问作者ives rodriguez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:40:57