Skip to content

Commit 7d1c821

Browse files
authored
Merge pull request apache#677 from apache/WW-5295-local-time
[WW-5295] Adds support for java.time.LocalTime to <s:date/> tag
2 parents 365f39e + 34208e6 commit 7d1c821

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

core/src/main/java/org/apache/struts2/components/Date.java

+2
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ public boolean end(Writer writer, String body) {
308308
date = ((LocalDateTime) dateObject).atZone(tz);
309309
} else if (dateObject instanceof LocalDate) {
310310
date = ((LocalDate) dateObject).atStartOfDay(tz);
311+
} else if (dateObject instanceof LocalTime) {
312+
date = ((LocalTime) dateObject).atDate(ZonedDateTime.now(tz).toLocalDate()).atZone(tz);
311313
} else if (dateObject instanceof Instant) {
312314
date = ((Instant) dateObject).atZone(tz);
313315
} else {

core/src/test/java/org/apache/struts2/components/DateTest.java

+25
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.io.Writer;
2929
import java.text.DateFormat;
3030
import java.text.SimpleDateFormat;
31+
import java.time.format.DateTimeFormatter;
3132
import java.util.Map;
3233

3334
public class DateTest extends StrutsInternalTestCase {
@@ -127,6 +128,30 @@ public void testJavaSqlTime() {
127128
assertEquals(expected, writer.toString());
128129
}
129130

131+
public void testJavaLocalTime() {
132+
// given
133+
Date date = new Date(stack);
134+
date.setDateFormatter(new SimpleDateFormatAdapter());
135+
136+
java.time.LocalTime now = java.time.LocalTime.now();
137+
138+
String timeFormat = "hh:mm:ss";
139+
String expected = DateTimeFormatter.ofPattern(timeFormat).format(now);
140+
context.put("myTime", now);
141+
142+
Writer writer = new StringWriter();
143+
144+
// when
145+
date.setName("myTime");
146+
date.setNice(false);
147+
date.setFormat(timeFormat);
148+
date.start(writer);
149+
date.end(writer, "");
150+
151+
// then
152+
assertEquals(expected, writer.toString());
153+
}
154+
130155
private DateFormat prepareFormat() {
131156
return SimpleDateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, ActionContext.getContext().getLocale());
132157
}

core/src/test/java/org/apache/struts2/views/jsp/ui/DateTagTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.time.Instant;
3131
import java.time.LocalDate;
3232
import java.time.LocalDateTime;
33+
import java.time.LocalTime;
3334
import java.time.ZoneId;
3435
import java.time.format.DateTimeFormatter;
3536
import java.util.Calendar;
@@ -993,6 +994,27 @@ public void testNewJava8Format() throws Exception {
993994
strutsBodyTagsAreReflectionEqual(tag, freshTag));
994995
}
995996

997+
public void testJavaLocalTime() throws Exception {
998+
String format = "hh:mm";
999+
LocalTime now = LocalTime.now();
1000+
String formatted = DateTimeFormatter.ofPattern(format, ActionContext.getContext().getLocale()).format(now);
1001+
context.put("myTime", now);
1002+
1003+
tag.setName("myTime");
1004+
tag.setNice(false);
1005+
tag.setFormat(format);
1006+
tag.doStartTag();
1007+
tag.doEndTag();
1008+
assertEquals(formatted, writer.toString());
1009+
1010+
// Basic sanity check of clearTagStateForTagPoolingServers() behaviour for Struts Tags after doEndTag().
1011+
DateTag freshTag = new DateTag();
1012+
freshTag.setPageContext(pageContext);
1013+
assertFalse("Tag state after doEndTag() under default tag clear state is equal to new Tag with pageContext/parent set. " +
1014+
"May indicate that clearTagStateForTagPoolingServers() calls are not working properly.",
1015+
strutsBodyTagsAreReflectionEqual(tag, freshTag));
1016+
}
1017+
9961018
/**
9971019
* Utility method to create a new {@link DateTextFieldTag} instance for code coverage tests.
9981020
* <p>

0 commit comments

Comments
 (0)