mirror of
https://github.com/apache/struts.git
synced 2026-08-07 15:46:57 +00:00
WW-1792 Date tag doesn't handle calendars
git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@539926 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -25,6 +25,7 @@ import java.io.Writer;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
@@ -274,9 +275,14 @@ public class Date extends Component {
|
||||
String msg = null;
|
||||
ValueStack stack = getStack();
|
||||
java.util.Date date = null;
|
||||
// find the name on the valueStack, and cast it to a date
|
||||
// find the name on the valueStack
|
||||
try {
|
||||
date = (java.util.Date) findValue(name);
|
||||
//suport Calendar also
|
||||
Object dateObject = findValue(name);
|
||||
if (dateObject instanceof java.util.Date)
|
||||
date = (java.util.Date) dateObject;
|
||||
else if(dateObject instanceof Calendar)
|
||||
date = ((Calendar) dateObject).getTime();
|
||||
} catch (Exception e) {
|
||||
LOG.error("Could not convert object with key '" + name
|
||||
+ "' to a java.util.Date instance");
|
||||
|
||||
@@ -51,6 +51,20 @@ public class DateTagTest extends AbstractTagTest {
|
||||
tag.doEndTag();
|
||||
assertEquals(formatted, writer.toString());
|
||||
}
|
||||
|
||||
public void testCustomFormatCalendar() throws Exception {
|
||||
String format = "yyyy/MM/dd hh:mm:ss";
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
String formatted = new SimpleDateFormat(format).format(calendar.getTime());
|
||||
context.put("myDate", calendar);
|
||||
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user