Apache Nutch is an extensible and scalable web crawler
NUTCH-2847 HttpDateFormat: Simplify based on new Java 8 DateTime API
Sebastian Nagelabc808390d6a
diff --git a/src/java/org/apache/nutch/net/protocols/HttpDateFormat.java b/src/java/org/apache/nutch/net/protocols/HttpDateFormat.javaindex f30fb2007..14447b5c9 100644--- a/src/java/org/apache/nutch/net/protocols/HttpDateFormat.java+++ b/src/java/org/apache/nutch/net/protocols/HttpDateFormat.java@@ -19,9 +19,13 @@ package org.apache.nutch.net.protocols;import java.util.Calendar;import java.util.Date;import java.util.Locale;-import java.util.TimeZone;-import java.text.SimpleDateFormat;import java.text.ParseException;+import java.time.Instant;+import java.time.ZoneId;+import java.time.ZoneOffset;+import java.time.ZonedDateTime;+import java.time.format.DateTimeFormatter;+import java.time.format.DateTimeParseException;/*** Parse and format HTTP dates in HTTP headers, e.g., used to fill the@@ -41,61 +45,47 @@ import java.text.ParseException;*/public class HttpDateFormat {- protected static SimpleDateFormat format = new SimpleDateFormat(- "EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);-- protected static TimeZone gmt = TimeZone.getTimeZone("GMT");+ public static final DateTimeFormatter FORMAT = DateTimeFormatter+ .ofPattern("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US)+ .withZone(ZoneId.of(ZoneOffset.UTC.toString()));/**- * HTTP date uses TimeZone GMT+ * Use a less restrictive format for parsing: accept single-digit day-of-month+ * and any timezone*/- static {- format.setTimeZone(gmt);- }+ public static final DateTimeFormatter PARSE_FORMAT = DateTimeFormatter+ .ofPattern("EEE, d MMM yyyy HH:mm:ss z", Locale.US)+ .withZone(ZoneId.of(ZoneOffset.UTC.toString()));/*** Get the HTTP format of the specified date.*/public static String toString(Date date) {- String string;- synchronized (format) {- string = format.format(date);- }- return string;+ return FORMAT.format(date.toInstant());}public static String toString(Calendar cal) {- String string;- synchronized (format) {- string = format.format(cal.getTime());- }- return string;+ return FORMAT.format(cal.toInstant());}- public static String toString(long time) {- String string;- synchronized (format) {- string = format.format(new Date(time));+ public static String toString(long millis) {+ return FORMAT.format(Instant.ofEpochMilli(millis));+ }++ public static ZonedDateTime toZonedDateTime(String dateString) throws ParseException {+ try {+ return PARSE_FORMAT.parse(dateString, ZonedDateTime::from);+ } catch (DateTimeParseException ex) {+ throw new ParseException(ex.getMessage(), 0);}- return string;}public static Date toDate(String dateString) throws ParseException {- Date date;- synchronized (format) {- date = format.parse(dateString);- format.setTimeZone(gmt);- }- return date;+ return Date.from(toZonedDateTime(dateString).toInstant());}public static long toLong(String dateString) throws ParseException {- long time;- synchronized (format) {- time = format.parse(dateString).getTime();- format.setTimeZone(gmt);- }- return time;+ return toZonedDateTime(dateString).toInstant().toEpochMilli();}public static void main(String[] args) throws Exception {