1

I'm using the Struts2-jQuery Datepicker with the Timepicker Addon.

Assuming that a java.util.Date object is returned form an Action getMyDate() getter method:

<sj:datepicker displayFormat="dd/mm/yy" 
            timepickerFormat="HH:mm"
                  timepicker="true" 
                        name="myDate" />

This always results in the date being populated correctly, but the timepicker is always 00:00.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • It seemed to work but it's not. I've found the JIRA describing it as a bug, I'm now searching for a workaround solution to this before reopening the answer – Andrea Ligios Dec 18 '14 at 13:58
  • Could you post a link to jira ticket. Maybe problem comes from specific format `HH:mm` try with `hh:mm`. – Aleksandr M Dec 18 '14 at 14:04
  • Which version of S2 jquery plugin? For me (with v3.7.0) your code with `` is working fine. – Aleksandr M Dec 18 '14 at 14:28
  • LOL... the problem I had was the same of the [Issue 1057](https://code.google.com/p/struts2-jquery/issues/detail?id=1057), I was sending a String but needed to parse a Date, not a String... so `name="myDateStr"` to a setter with parsing capabilities (if not using a converter) and `value="myDate"`. P.S: 3.7.1 – Andrea Ligios Dec 18 '14 at 14:41

1 Answers1

1

Ok, I've found the time to expand the solution I've figured out on 18 december and briefly mentioned in the comments.

The Problem

The <sj:datepicker timepicker="true"> tag correctly parses a Date object, but not its String representation. This is reported in Issue 1057, with status accepted but still not fixed.

This works:

<sj:datepicker displayFormat="dd/mm/yy" 
                  timepicker="true" 
                       value="%{new java.util.Date()}" />

This does NOT work:

<sj:datepicker displayFormat="dd/mm/yy" 
                  timepicker="true" 
                       value="%{'07/01/2015 14:42'}" />

When not using a Converter, you need to send the format of the date-time to a String variable, instead than to a Date one:

<sj:datepicker displayFormat="dd/mm/yy" 
                  timepicker="true" 
                        name="dateStr" />
private String dateStr; 
public void setDateStr(String dateStr){
    this.dateStr = dateStr;
}
public String getDateStr(){
    return dateStr;
}

If you need a Date (you probably do), you can use a String Getter and Setter, that will handle the parsing and the formatting between Date and String:

private static final String FORMAT = "dd/MM/yyyy HH:mm";

private Date date; 
public void setDateStr(String dateStr){
    date = new SimpleDateFormat(FORMAT,Locale.ITALIAN).parse(dateStr);
}
public String getDateStr(){
    return new SimpleDateFormat(FORMAT,Locale.ITALIAN).format(date);
}

But both this methods, due to the bug linked above, won't work. The solution is so easy that made me laugh in my last comment to the question:

The Solution

Use a String setter, and a Date getter, then it will work perfectly:

<sj:datepicker displayFormat="dd/mm/yy" 
                  timepicker="true" 
                        name="dateStr" 
                       value="date" />
private Date date; 
public void setDateStr(String dateStr){
    date = new SimpleDateFormat(FORMAT,Locale.ITALIAN).parse(dateStr);
}
public Date getDate(){
    return date;
}

(or write a Converter).

Side note: timepickerFormat="HH:mm" is not needed because HH:mm (24 hours format) is already the default format. You need to change it only if you want the 12 hours display format.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243