Search code examples
jsptimestruts2datetimepickerstruts2-jquery

How to display the time in 24-hour format using Struts 2


I'm using Struts 2 and want to display time in the 24-hour format, but it is showing in the 12-hour format.

<sx:datetimepicker name="LoginTime" 
                   type="time" 
                   displayFormat = "HH:mm" 
                   label="Login Time(HH:MM 24hours format)" />

Solution

  • The format "HH" is used in Java for 24 hours, "hh" for 12 hours.

    As of sx: prefix commonly used for Struts2 Dojo tags, you'd better replace it with "sj:" and Struts2 jQuery plugin. It is using its own format for displaying the value. The DatePicker widget displayFormat attribute should be used to define the format of the date. If it doesn't fit the required format you have also an option to format the value in topic. To display a date and time you need a timepicker addon.

    <%@ taglib prefix="s" uri="/struts-tags"%>
    <%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
    <html>
      <head>
        <sj:head locale="de" jquerytheme="lightness"/>
      </head>
      <body>
        <s:form id="form" theme="xhtml">
          <sj:datetimepicker name="LoginTime" timepicker="true" timepickerOnly="true"  timepickerFormat="hh:mm" label="Login Time(HH:MM 24hours format)"/>
        </s:form>
      </body>
    </html>