Skip to content

Commit

Permalink
#444 using UTC timezone for formatting java.util.Date and millisecond…
Browse files Browse the repository at this point in the history
…s parameters
  • Loading branch information
svendiedrichsen committed Dec 18, 2017
1 parent a491b0b commit 9322fe8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
21 changes: 11 additions & 10 deletions src/main/java/org/cactoos/time/DateAsText.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;
Expand All @@ -41,6 +41,7 @@
* @version $Id$
* @since 1.0
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public class DateAsText implements Text {
/**
* Scalar carrying the formatted date.
Expand All @@ -53,8 +54,8 @@ public class DateAsText implements Text {
*/
public DateAsText(final long milliseconds) {
this(
LocalDateTime.ofInstant(
Instant.ofEpochMilli(milliseconds), ZoneId.systemDefault()
ZonedDateTime.ofInstant(
Instant.ofEpochMilli(milliseconds), ZoneId.of("UTC")
),
DateTimeFormatter.ISO_DATE_TIME
);
Expand All @@ -67,8 +68,8 @@ public DateAsText(final long milliseconds) {
*/
public DateAsText(final long milliseconds, final String format) {
this(
LocalDateTime.ofInstant(
Instant.ofEpochMilli(milliseconds), ZoneId.systemDefault()
ZonedDateTime.ofInstant(
Instant.ofEpochMilli(milliseconds), ZoneId.of("UTC")
),
format,
Locale.getDefault(Locale.Category.FORMAT)
Expand All @@ -84,8 +85,8 @@ public DateAsText(final long milliseconds, final String format) {
public DateAsText(final long milliseconds, final String format,
final Locale locale) {
this(
LocalDateTime.ofInstant(
Instant.ofEpochMilli(milliseconds), ZoneId.systemDefault()
ZonedDateTime.ofInstant(
Instant.ofEpochMilli(milliseconds), ZoneId.of("UTC")
),
DateTimeFormatter.ofPattern(format, locale)
);
Expand All @@ -97,7 +98,7 @@ public DateAsText(final long milliseconds, final String format,
*/
public DateAsText(final Date date) {
this(
LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()),
ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC")),
DateTimeFormatter.ISO_DATE_TIME
);
}
Expand All @@ -110,7 +111,7 @@ public DateAsText(final Date date) {
*/
public DateAsText(final Date date, final String format) {
this(
LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()),
ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC")),
DateTimeFormatter.ofPattern(
format,
Locale.getDefault(Locale.Category.FORMAT)
Expand All @@ -128,7 +129,7 @@ public DateAsText(final Date date, final String format) {
public DateAsText(final Date date, final String format,
final Locale locale) {
this(
LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()),
ZonedDateTime.ofInstant(date.toInstant(), ZoneId.of("UTC")),
DateTimeFormatter.ofPattern(format, locale)
);
}
Expand Down
23 changes: 15 additions & 8 deletions src/test/java/org/cactoos/time/DateAsTextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
Expand Down Expand Up @@ -84,19 +85,21 @@ public final void testZonedDateTimeFormattedWithFormatStringWithLocale()

@Test
public final void testDateFormattedUsingIsoFormatter() throws Exception {
final Calendar calendar = Calendar.getInstance();
final Calendar calendar =
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16);
calendar.set(Calendar.MILLISECOND, 17);
MatcherAssert.assertThat(
"Can't format a java.util.Date with ISO format.",
new DateAsText(calendar.getTime()).asString(),
Matchers.is("2017-12-13T14:15:16.017")
Matchers.is("2017-12-13T14:15:16.017Z[UTC]")
);
}

@Test
public final void testDateFormattedUsingCustomFormat() throws Exception {
final Calendar calendar = Calendar.getInstance();
final Calendar calendar =
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16);
MatcherAssert.assertThat(
"Can't format a java.util.Date with custom format.",
Expand All @@ -110,7 +113,8 @@ public final void testDateFormattedUsingCustomFormat() throws Exception {
@Test
public final void testDateFormattedUsingCustomFormatDifferentLocale()
throws Exception {
final Calendar calendar = Calendar.getInstance();
final Calendar calendar =
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16);
MatcherAssert.assertThat(
"Can't format a java.util.Date with custom format.",
Expand All @@ -124,20 +128,22 @@ public final void testDateFormattedUsingCustomFormatDifferentLocale()
@Test
public final void testMillisFormattedUsingIsoFormatter()
throws Exception {
final Calendar calendar = Calendar.getInstance();
final Calendar calendar =
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16);
calendar.set(Calendar.MILLISECOND, 17);
MatcherAssert.assertThat(
"Can't format a java.util.Date with ISO format.",
new DateAsText(calendar.getTime().getTime()).asString(),
Matchers.is("2017-12-13T14:15:16.017")
Matchers.is("2017-12-13T14:15:16.017Z[UTC]")
);
}

@Test
public final void testMillisFormattedUsingCustomFormat()
throws Exception {
final Calendar calendar = Calendar.getInstance();
final Calendar calendar =
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16);
MatcherAssert.assertThat(
"Can't format a java.util.Date with custom format.",
Expand All @@ -152,7 +158,8 @@ public final void testMillisFormattedUsingCustomFormat()
@Test
public final void testMillisFormattedUsingCustomFormatDifferentLocale()
throws Exception {
final Calendar calendar = Calendar.getInstance();
final Calendar calendar =
Calendar.getInstance(TimeZone.getTimeZone("UTC"));
calendar.set(2017, Calendar.DECEMBER, 13, 14, 15, 16);
MatcherAssert.assertThat(
"Can't format a java.util.Date with custom format.",
Expand Down

0 comments on commit 9322fe8

Please sign in to comment.