From 406d72ea371b95e30be68a511b9c516b67d34fd3 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 1 Nov 2018 13:03:27 +0300 Subject: RFC1123 http date formatter (2DIGIT day-of-month) --- .../src/main/java/com/juick/util/DateFormatter.java | 7 ++++--- .../main/java/com/juick/util/DateFormattersHolder.java | 15 +++++++++++++++ juick-common/src/test/java/com/juick/FormatterTest.java | 1 + 3 files changed, 20 insertions(+), 3 deletions(-) (limited to 'juick-common/src') diff --git a/juick-common/src/main/java/com/juick/util/DateFormatter.java b/juick-common/src/main/java/com/juick/util/DateFormatter.java index f9e23a91..8f569562 100644 --- a/juick-common/src/main/java/com/juick/util/DateFormatter.java +++ b/juick-common/src/main/java/com/juick/util/DateFormatter.java @@ -22,6 +22,7 @@ import org.apache.commons.lang3.StringUtils; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Locale; @@ -39,7 +40,7 @@ public class DateFormatter { if (ts == null) return null; - LocalDateTime ldt = LocalDateTime.ofInstant(ts, ZoneOffset.UTC); + ZonedDateTime ldt = ZonedDateTime.ofInstant(ts, ZoneOffset.UTC); return ldt.format(formatter); } @@ -49,8 +50,8 @@ public class DateFormatter { if (StringUtils.isBlank(v)) return null; - LocalDateTime ldt = LocalDateTime.parse(v, formatter); + ZonedDateTime ldt = ZonedDateTime.parse(v, formatter); - return ldt.toInstant(ZoneOffset.UTC); + return ldt.toInstant(); } } diff --git a/juick-common/src/main/java/com/juick/util/DateFormattersHolder.java b/juick-common/src/main/java/com/juick/util/DateFormattersHolder.java index 09fd17d7..8292e68e 100644 --- a/juick-common/src/main/java/com/juick/util/DateFormattersHolder.java +++ b/juick-common/src/main/java/com/juick/util/DateFormattersHolder.java @@ -57,4 +57,19 @@ public class DateFormattersHolder { } return localInstance; } + + private static volatile DateFormatter httpDateFormatter; + + public static DateFormatter getHttpDateFormatter() { + DateFormatter localInstance = httpDateFormatter; + if (localInstance == null) { + synchronized (DateFormatter.class) { + localInstance = httpDateFormatter; + if (localInstance == null) { + httpDateFormatter = localInstance = new DateFormatter("EEE, dd MMM yyyy HH:mm:ss O"); + } + } + } + return localInstance; + } } diff --git a/juick-common/src/test/java/com/juick/FormatterTest.java b/juick-common/src/test/java/com/juick/FormatterTest.java index 397c4b6c..da9f5d26 100644 --- a/juick-common/src/test/java/com/juick/FormatterTest.java +++ b/juick-common/src/test/java/com/juick/FormatterTest.java @@ -59,5 +59,6 @@ public class FormatterTest { assertThat(DateFormattersHolder.getMessageFormatterInstance().format(date.toInstant()), equalTo("2012-01-01 00:00:00")); assertThat(DateFormattersHolder.getRssFormatterInstance().format(date.toInstant()), equalTo("Sun, 1 Jan 2012 00:00:00")); + assertThat(DateFormattersHolder.getHttpDateFormatter().format(date.toInstant()), equalTo("Sun, 01 Jan 2012 00:00:00 GMT")); } } -- cgit v1.2.3