You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@struts.apache.org by lu...@apache.org on 2022/12/28 09:13:11 UTC
[struts] branch WW-5272-time created (now df110596c)
This is an automated email from the ASF dual-hosted git repository.
lukaszlenart pushed a change to branch WW-5272-time
in repository https://gitbox.apache.org/repos/asf/struts.git
at df110596c WW-5272 Extends <s:date/> to support java.sql.Time
This branch includes the following new commits:
new df110596c WW-5272 Extends <s:date/> to support java.sql.Time
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[struts] 01/01: WW-5272 Extends to support java.sql.Time
Posted by lu...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
lukaszlenart pushed a commit to branch WW-5272-time
in repository https://gitbox.apache.org/repos/asf/struts.git
commit df110596c57318935a10665529eaf6b3f215b4fa
Author: Lukasz Lenart <lu...@apache.org>
AuthorDate: Wed Dec 28 10:13:04 2022 +0100
WW-5272 Extends <s:date/> to support java.sql.Time
---
.../java/org/apache/struts2/components/Date.java | 5 +++-
.../components/date/SimpleDateFormatAdapter.java | 2 +-
.../org/apache/struts2/components/DateTest.java | 32 ++++++++++++++++++++--
3 files changed, 35 insertions(+), 4 deletions(-)
diff --git a/core/src/main/java/org/apache/struts2/components/Date.java b/core/src/main/java/org/apache/struts2/components/Date.java
index 7a8cda061..de640cbda 100644
--- a/core/src/main/java/org/apache/struts2/components/Date.java
+++ b/core/src/main/java/org/apache/struts2/components/Date.java
@@ -32,6 +32,7 @@ import java.io.Writer;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
+import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
@@ -294,7 +295,9 @@ public class Date extends ContextBean {
// find the name on the valueStack
Object dateObject = findValue(name);
if (dateObject instanceof java.sql.Date) {
- date = ((java.sql.Date) dateObject).toLocalDate().atStartOfDay(tz);
+ date = ((java.sql.Date) dateObject).toLocalDate().atTime(LocalTime.now(tz)).atZone(tz);
+ } else if (dateObject instanceof java.sql.Time) {
+ date = ((java.sql.Time) dateObject).toLocalTime().atDate(ZonedDateTime.now(tz).toLocalDate()).atZone(tz);
} else if (dateObject instanceof java.util.Date) {
date = ((java.util.Date) dateObject).toInstant().atZone(tz);
} else if (dateObject instanceof Calendar) {
diff --git a/core/src/main/java/org/apache/struts2/components/date/SimpleDateFormatAdapter.java b/core/src/main/java/org/apache/struts2/components/date/SimpleDateFormatAdapter.java
index 38f3c5296..7bea206f2 100644
--- a/core/src/main/java/org/apache/struts2/components/date/SimpleDateFormatAdapter.java
+++ b/core/src/main/java/org/apache/struts2/components/date/SimpleDateFormatAdapter.java
@@ -34,7 +34,7 @@ public class SimpleDateFormatAdapter implements DateFormatter {
DateFormat df;
Locale locale = ActionContext.getContext().getLocale();
if (format == null) {
- df = SimpleDateFormat.getDateInstance(DateFormat.MEDIUM, locale);
+ df = SimpleDateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
} else {
df = new SimpleDateFormat(format, locale);
}
diff --git a/core/src/test/java/org/apache/struts2/components/DateTest.java b/core/src/test/java/org/apache/struts2/components/DateTest.java
index ffe01a52a..477b128a6 100644
--- a/core/src/test/java/org/apache/struts2/components/DateTest.java
+++ b/core/src/test/java/org/apache/struts2/components/DateTest.java
@@ -66,7 +66,7 @@ public class DateTest extends StrutsInternalTestCase {
java.util.Date now = new java.util.Date();
- String expected = SimpleDateFormat.getDateInstance(DateFormat.MEDIUM, ActionContext.getContext().getLocale()).format(now);
+ String expected = prepareFormat().format(now);
context.put("myDate", now);
Writer writer = new StringWriter();
@@ -88,7 +88,7 @@ public class DateTest extends StrutsInternalTestCase {
java.sql.Date now = new java.sql.Date(System.currentTimeMillis());
- String expected = SimpleDateFormat.getDateInstance(DateFormat.MEDIUM, ActionContext.getContext().getLocale()).format(now);
+ String expected = prepareFormat().format(now);
context.put("myDate", now);
Writer writer = new StringWriter();
@@ -103,6 +103,34 @@ public class DateTest extends StrutsInternalTestCase {
assertEquals(expected, writer.toString());
}
+ public void testJavaSqlTime() {
+ // given
+ Date date = new Date(stack);
+ date.setDateFormatter(new SimpleDateFormatAdapter());
+
+ java.sql.Time now = new java.sql.Time(System.currentTimeMillis());
+
+ String timeFormat = "hh:mm:ss";
+ String expected = new SimpleDateFormat(timeFormat, ActionContext.getContext().getLocale()).format(now);
+ context.put("myDate", now);
+
+ Writer writer = new StringWriter();
+
+ // when
+ date.setName("myDate");
+ 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());
+ }
+
@Override
public void setUp() throws Exception {
super.setUp();