Skip to content

Commit

Permalink
WW-4705 - Add support for long type to <s:date> tag
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandr-m committed Nov 1, 2016
1 parent 8699f63 commit 8fe2bb8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
8 changes: 5 additions & 3 deletions core/src/main/java/org/apache/struts2/components/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,10 @@ public boolean end(Writer writer, String body) {
Object dateObject = findValue(name);
if (dateObject instanceof java.util.Date) {
date = (java.util.Date) dateObject;
} else if(dateObject instanceof Calendar){
} else if (dateObject instanceof Calendar) {
date = ((Calendar) dateObject).getTime();
} else if (dateObject instanceof Long) {
date = new java.util.Date((long) dateObject);
} else {
if (devMode) {
String developerNotification = LocalizedTextUtil.findText(
Expand All @@ -299,12 +301,12 @@ public boolean end(Writer writer, String body) {
"Developer Notification:\n{0}",
new Object[]{
"Expression [" + name + "] passed to <s:date/> tag which was evaluated to [" + dateObject + "]("
+ (dateObject != null ? dateObject.getClass() : "null") + ") isn't instance of java.util.Date nor java.util.Calendar!"
+ (dateObject != null ? dateObject.getClass() : "null") + ") isn't instance of java.util.Date nor java.util.Calendar nor long!"
}
);
LOG.warn(developerNotification);
} else {
LOG.debug("Expression [{}] passed to <s:date/> tag which was evaluated to [{}]({}) isn't instance of java.util.Date nor java.util.Calendar!",
LOG.debug("Expression [{}] passed to <s:date/> tag which was evaluated to [{}]({}) isn't instance of java.util.Date nor java.util.Calendar nor long!",
name, dateObject, (dateObject != null ? dateObject.getClass() : "null"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ public void testCustomFormatCalendar() throws Exception {
assertEquals(formatted, writer.toString());
}

public void testCustomFormatLong() throws Exception {
String format = "yyyy/MM/dd hh:mm:ss";
Date date = new Date();
String formatted = new SimpleDateFormat(format).format(date);
// long
context.put("myDate", date.getTime());

tag.setName("myDate");
tag.setNice(false);
tag.setFormat(format);
tag.doStartTag();
tag.doEndTag();
assertEquals(formatted, writer.toString());
}

public void testDefaultFormat() throws Exception {
Date now = new Date();
String formatted = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM,
Expand Down

0 comments on commit 8fe2bb8

Please sign in to comment.