Reputation: 1219
I am using Struts 2 Jquery Date picker for selection of date.
Like below
<sj:datepicker name="testDate"
parentTheme="simple"
zindex="2006"
timepicker="true"
changeYear="true"
displayFormat="dd/mm/yy"
timepickerFormat="HH:mm:ss"
timepickerShowSecond="true"
duration="fast"
readonly="true"
></sj:datepicker>
If you can see the format I mentioned there is dd/mm/yy
and time format is HH:mm:ss
, here everything is working fine when the user selecting the date ..
When User selects the date I am getting date In expected format in my Action class. But When I redirect to same page after completing the action my date pickers text fields are being set by the wrong format like this mm/dd/yyy
.. I want my date datepickers formats to be set to the date format I mentioned in my datepicker
tag.
Becuase of this user is getting confused can any one tell me how to set the same format what the user has selected before the action even after the action completed
I m using struts2 jquery plugin
Upvotes: 3
Views: 5434
Reputation: 14170
I think that by setting the value of the Datepicker using a Java Date (instead of String) you should be fine:
<sj:datepicker name="testDate"
value="%{testDateValue}"
...
></sj:datepicker>
and in the action class:
public Date getTestDateValue()
{
try {
return new SimpleDateFormat("dd/MM/yy HH:mm:ss").parse(getTestDate());
} catch (ParseException e) {
return null;
}
}
Upvotes: 0
Reputation: 2856
The problem is because dd/mm/yy
is not a valid format according to struts2 jquery
.
Valid format is dd.mm.yy
or dd-mm-yy
Valid Formats according to this link are: Click here
dd.mm.yy
mm/dd/yy
DD, d MM yy
d M, yy
dd-mm-yy
They say
displayFormat
--> A pattern used for the visual display of the formatted date, e.g. yy- mm-dd , dd.mm.yy
But pattern dd/mm/yy
is not working. They also did not specified /
(Slash) in dd/mm/yy
description of displayFormat. They specified .
(Dot) and -
(Dash) only.
I have also tried with dd//mm//yy
which is working!!!
But not working with dd/mm/yy
:(
Reason:
datepicker can't parse a string value dd/mm/yy
. It is treating dd/mm/yy
as mm/dd/yy
.
For some values it fails in which it treats maximum value between day and month as day when both of them are less than 12.
For Example:
Input: 02/07/2014 09:30:41
displayFormat: dd/mm/yy
Actual Day : 2
Actual Month : 7
Actual Time: 09:30:41Parsedday : 7
Parsedmonth : 2
Parsedtime : : 00:00:00Output: 07/02/2014 00:00:00
This is an issue which is open and still not solved. Struts2 Jquery Team have accepted that this is an issue. The status is accepted of this issue.
Here is the issue link
Issue 1057 in struts2-jquery: datepicker can't parse a string value with a time component
I will suggest you to use
displayFormat="dd-mm-yy"
to solve your problem or wait for the issue 1057 to get solved.
According to my research Following function needs to be debugged with alert(err);
Actual function that parses is
//#######################################################################################
// Splits datetime string into date ans time substrings.
// Throws exception when date can't be parsed
// If only date is present, time substring eill be ''
//#######################################################################################
var splitDateTime = function(dateFormat, dateTimeString, dateSettings)
{
try {
var date = $.datepicker._base_parseDate(dateFormat, dateTimeString, dateSettings);
} catch (err) {
if (err.indexOf(":") >= 0) {
// Hack! The error message ends with a colon, a space, and
// the "extra" characters. We rely on that instead of
// attempting to perfectly reproduce the parsing algorithm.
var dateStringLength = dateTimeString.length-(err.length-err.indexOf(':')-2);
var timeString = dateTimeString.substring(dateStringLength);
return [dateTimeString.substring(0, dateStringLength), dateTimeString.substring(dateStringLength)];
} else {
throw err;
}
}
return [dateTimeString, ''];
};
Source Code Link: Line 1447 jquery-ui-timepicker-addon.js
//#######################################################################################
// override parseDate() because UI 1.8.14 throws an error about "Extra characters"
// An option in datapicker to ignore extra format characters would be nicer.
//#######################################################################################
$.datepicker._base_parseDate = $.datepicker.parseDate;
$.datepicker.parseDate = function(format, value, settings) {
var splitRes = splitDateTime(format, value, settings);
return $.datepicker._base_parseDate(format, splitRes[0], settings);
};
Source Code Link: Line 1340 jquery-ui-timepicker-addon.js
Upvotes: 3