当我尝试编辑日期和时间时,我不能。它显示错误:
无法将给定对象格式化为日期。
这是RegistroBean
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:Sun May 27 27:00:00 COT 2018
hora_in:Thu Jan 01 06:15:30 COT 1970
hora_out:Thu Jan 01 14:30:00 COT 1970
应以此格式显示日期和时间
fecha:05/27/2018
hora_in:06:15:30
hora_out:14:30:00
我应该更改什么才能将日期转换为字符串而不会出现任何错误?
答案 0 :(得分:1)
我放弃了理解你的方法脱离背景。我认为你正在追求以下内容。
DateTimeFormatter dateFormatter = DateTimeFormatter. ofPattern("MM/dd/yyyy");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss X");
ZoneOffset offset = ZoneOffset.UTC;
LocalDate fecha = LocalDate.now(offset);
OffsetTime horaIn = OffsetTime.of(15, 0, 0, 0, offset);
OffsetTime horaOut = OffsetTime.of(17, 37, 15, 0, offset);
String strDate = fecha.format(dateFormatter);
System.out.println(strDate);
String strHourIn = horaIn.format(timeFormatter);
System.out.println(strHourIn);
String strHourOut = horaOut.format(timeFormatter);
System.out.println(strHourOut);
此代码打印:
05/25/2018
15:00:00 Z
17:37:15 Z
我使用的是现代Java日期和时间API java.time
。众所周知SimpleDateFormat
很麻烦,而且Date
和DateFormat
已经过时了。我没有在时间格式中对字母Z
进行硬编码,而是希望将偏移量放入我格式化的数据中,这样如果有一天我们决定使用另一个偏移量({{1} } class很少使用,但在这里似乎是正确的。)
如果您的时间为OffsetTime
,即没有偏移量,例如从数据库中获得,请按以下方式进行转换:
LocalTime
链接: Oracle tutorial: Date Time解释如何使用 OffsetTime hora = localTimeFromDatabase.atOffset(offset);
。