【问题标题】:I cannot edit date and time我无法编辑日期和时间
【发布时间】: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。该类是出了名的麻烦,并且与DateDateFormat 一起早已过时。改为查看java.time, the modern Java date and time API,

标签: java date jsf formatting


【解决方案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.timeSimpleDateFormat 是出了名的麻烦,并且与 DateDateFormat 一起早已过时。与其以时间格式硬编码字母Z,我更喜欢将偏移量放入我格式化的数据中,这样如果有一天我们决定使用另一个偏移量(OffsetTime 类很少使用过,但在这里似乎是正确的)。

如果你得到你的时间为LocalTime,也就是说,没有偏移,例如从你的数据库,转换如下:

    OffsetTime hora = localTimeFromDatabase.atOffset(offset);

链接:Oracle tutorial: Date Time解释如何使用java.time

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 2023-03-11
    • 1970-01-01
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    相关资源
    最近更新 更多