【发布时间】:2018-05-25 02:25:18
【问题描述】:
当我尝试编辑日期和时间时,我不能。它显示错误:
无法将给定的对象格式化为日期。
这是注册豆
public String Editar(Integer id){
Registros r=this.registrosFacade.find(id);
DateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
String strDate = dateFormatter.format(fecha);
fecha = strDate;
this.fecha=r.getFecha().toString();
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss 'Z'");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
String strHour = dateFormat.format(hora_in);
hora_in = strHour;
this.hora_in=r.getHoraIn().toString();
String strHour2 = dateFormat.format(hora_out);
hora_out = strHour2;
this.hora_out=r.getHoraOut().toString();
this.vehiculo=r.getIdVehiculo();
return "RegistroEdit";
}
public String GuardarEdicion(RegistroController rc, int id) throws ParseException{
Registros r = new Registros();
Locale locale = new Locale("es","CO");
String datef = "MM/dd/yyyy";
SimpleDateFormat formatter = new SimpleDateFormat(datef, locale);
Date parsedDate = formatter.parse(fecha);
r.setFecha(parsedDate);
String hourf = "HH:mm:ss 'Z'";
SimpleDateFormat format = new SimpleDateFormat(hourf, locale);
format.setTimeZone(TimeZone.getTimeZone("GMT"));
Date Hin = format.parse(hora_in);
r.setHoraIn(Hin);
Date Hout = format.parse(hora_out);
r.setHoraOut(Hout);
r.setIdVehiculo(vehiculosFacade.find(vehiculo.getId()));
this.registrosFacade.edit(r);
return "RegistroList";
}
在我编辑的列表视图中,我点击了这个按钮:
<h:commandButton value="Editar" action="#{registroController.Editar(item.id)}"/>
这是 RegistroEdit.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<f:view>
<h:form>
<h1><h:outputText value="Editar Registro"/></h1>
<p:panelGrid columns="2">
<p:outputLabel value="Fecha:" for="fecha" />
<p:inputText id="fecha" value="#{registroController.fecha}" title="Fecha" >
</p:inputText>
<p:outputLabel value="HoraIn:" for="horaIn" />
<p:inputText id="horaIn" value="#{registroController.hora_in}" title="HoraIn" required="true" requiredMessage="The HoraIn field is required.">
</p:inputText>
<p:outputLabel value="HoraOut:" for="horaOut" />
<p:inputText id="horaOut" value="#{registroController.hora_out}" title="HoraOut" required="true" requiredMessage="The HoraOut field is required.">
</p:inputText>
<p:outputLabel value="IdVehiculo:" for="idVehiculo" />
<p:selectOneMenu id="idVehiculo" value="#{registroController.vehiculo.id}" required="true" requiredMessage="The IdVehiculo field is required.">
<!-- TODO: update below reference to list of available items-->
<f:selectItems value="#{vehiculoController.findAll()}" var="v" itemLabel="#{v.placa}" itemValue="#{v.id}"/>
</p:selectOneMenu>
</p:panelGrid>
<h:panelGrid columns="2">
<h:commandButton id="registroCommand" value="Guardar"
action="#{registroController.GuardarEdicion(registroController, registroController.id)}"/>
<h:commandButton id="registroCommand1" value="Ir a Lista"
action="#{registro.prepareList()}"/>
</h:panelGrid>
</h:form>
</f:view>
</h:body>
</html>
尝试编辑以这种格式显示在列表视图中的日期和时间:
fecha:2018 年 5 月 27 日星期日 00:00:00 COT
hora_in:星期四 1 月 1 日 06:15:30 COT 1970
hora_out:1970 年 1 月 1 日星期四 14:30:00 COT
应该以这种格式显示日期和时间
fecha:2018 年 5 月 27 日
hora_in: 06:15:30
hora_out:14:30:00
我应该改变什么才能将日期转换为字符串而不会出现任何错误?
【问题讨论】:
-
dateFormatter.format(fecha);这次好像没有设置fecha -
数据库的第一个我用 fecha 的 get 带了值,我不知道如何将日期转换为字符串
-
dateFormatter.format(dateObject) -
代码太多。只发布最低限度的内容以显示您的问题。
-
在这种情况下@ScaryWombat’s suggestion 有效。不过,我建议你不要使用
SimpleDateFormat。该类是出了名的麻烦,并且与Date和DateFormat一起早已过时。改为查看java.time, the modern Java date and time API,。
标签: java date jsf formatting