WW-4705 - Add support for long type to <s:date> tag

This commit is contained in:
Aleksandr Mashchenko
2016-11-01 19:01:44 +02:00
parent 8699f639f3
commit 8fe2bb8317
2 changed files with 20 additions and 3 deletions
@@ -288,8 +288,10 @@ public class Date extends ContextBean {
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(
@@ -299,12 +301,12 @@ public class Date extends ContextBean {
"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"));
}
}
@@ -102,6 +102,21 @@ public class DateTagTest extends AbstractTagTest {
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,