WW-5295 Adds support for java.time.LocalTime to <s:date/> tag

This commit is contained in:
Lukasz Lenart
2023-03-30 19:45:56 +02:00
parent 365f39e9f4
commit 34208e6960
3 changed files with 49 additions and 0 deletions
@@ -308,6 +308,8 @@ public class Date extends ContextBean {
date = ((LocalDateTime) dateObject).atZone(tz);
} else if (dateObject instanceof LocalDate) {
date = ((LocalDate) dateObject).atStartOfDay(tz);
} else if (dateObject instanceof LocalTime) {
date = ((LocalTime) dateObject).atDate(ZonedDateTime.now(tz).toLocalDate()).atZone(tz);
} else if (dateObject instanceof Instant) {
date = ((Instant) dateObject).atZone(tz);
} else {
@@ -28,6 +28,7 @@ import java.io.StringWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Map;
public class DateTest extends StrutsInternalTestCase {
@@ -127,6 +128,30 @@ public class DateTest extends StrutsInternalTestCase {
assertEquals(expected, writer.toString());
}
public void testJavaLocalTime() {
// given
Date date = new Date(stack);
date.setDateFormatter(new SimpleDateFormatAdapter());
java.time.LocalTime now = java.time.LocalTime.now();
String timeFormat = "hh:mm:ss";
String expected = DateTimeFormatter.ofPattern(timeFormat).format(now);
context.put("myTime", now);
Writer writer = new StringWriter();
// when
date.setName("myTime");
date.setNice(false);
date.setFormat(timeFormat);
date.start(writer);
date.end(writer, "");
// then
assertEquals(expected, writer.toString());
}
private DateFormat prepareFormat() {
return SimpleDateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, ActionContext.getContext().getLocale());
}
@@ -30,6 +30,7 @@ import java.text.DateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
@@ -993,6 +994,27 @@ public class DateTagTest extends AbstractTagTest {
strutsBodyTagsAreReflectionEqual(tag, freshTag));
}
public void testJavaLocalTime() throws Exception {
String format = "hh:mm";
LocalTime now = LocalTime.now();
String formatted = DateTimeFormatter.ofPattern(format, ActionContext.getContext().getLocale()).format(now);
context.put("myTime", now);
tag.setName("myTime");
tag.setNice(false);
tag.setFormat(format);
tag.doStartTag();
tag.doEndTag();
assertEquals(formatted, writer.toString());
// Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
DateTag freshTag = new DateTag();
freshTag.setPageContext(pageContext);
assertFalse("Tag state after doEndTag() under default tag clear state is equal to new Tag with pageContext/parent set. " +
"May indicate that clearTagStateForTagPoolingServers() calls are not working properly.",
strutsBodyTagsAreReflectionEqual(tag, freshTag));
}
/**
* Utility method to create a new {@link DateTextFieldTag} instance for code coverage tests.
* <p>