diff --git a/api/src/main/java/io/jsonwebtoken/lang/DateFormats.java b/api/src/main/java/io/jsonwebtoken/lang/DateFormats.java index a431b8b6..e8a67e14 100644 --- a/api/src/main/java/io/jsonwebtoken/lang/DateFormats.java +++ b/api/src/main/java/io/jsonwebtoken/lang/DateFormats.java @@ -4,6 +4,7 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.TimeZone; /** * @since 0.10.0 @@ -17,14 +18,18 @@ public class DateFormats { private static final ThreadLocal ISO_8601 = new ThreadLocal() { @Override protected DateFormat initialValue() { - return new SimpleDateFormat(ISO_8601_PATTERN); + SimpleDateFormat format = new SimpleDateFormat(ISO_8601_PATTERN); + format.setTimeZone(TimeZone.getTimeZone("UTC")); + return format; } }; private static final ThreadLocal ISO_8601_MILLIS = new ThreadLocal() { @Override protected DateFormat initialValue() { - return new SimpleDateFormat(ISO_8601_MILLIS_PATTERN); + SimpleDateFormat format = new SimpleDateFormat(ISO_8601_MILLIS_PATTERN); + format.setTimeZone(TimeZone.getTimeZone("UTC")); + return format; } }; diff --git a/api/src/test/groovy/io/jsonwebtoken/lang/DateFormatsTest.groovy b/api/src/test/groovy/io/jsonwebtoken/lang/DateFormatsTest.groovy new file mode 100644 index 00000000..bc928b94 --- /dev/null +++ b/api/src/test/groovy/io/jsonwebtoken/lang/DateFormatsTest.groovy @@ -0,0 +1,25 @@ +package io.jsonwebtoken.lang + +import org.junit.Test + +import java.text.SimpleDateFormat + +import static org.junit.Assert.* + +class DateFormatsTest { + + @Test //https://github.com/jwtk/jjwt/issues/291 + void testUtcTimezone() { + + def iso8601 = DateFormats.ISO_8601.get() + def iso8601Millis = DateFormats.ISO_8601_MILLIS.get() + + assertTrue iso8601 instanceof SimpleDateFormat + assertTrue iso8601Millis instanceof SimpleDateFormat + + def utc = TimeZone.getTimeZone("UTC") + + assertEquals utc, iso8601.getTimeZone() + assertEquals utc, iso8601Millis.getTimeZone() + } +}