Skip to content

Commit 202031f

Browse files
committed
Migrate DateParser to java.time, close AsyncHttpClient#1011
1 parent 1cc6f3d commit 202031f

File tree

4 files changed

+38
-77
lines changed

4 files changed

+38
-77
lines changed

client/src/main/java/org/asynchttpclient/cookie/CookieUtil.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
*/
1313
package org.asynchttpclient.cookie;
1414

15-
import java.text.ParsePosition;
1615
import java.util.BitSet;
1716
import java.util.Date;
1817

@@ -97,7 +96,7 @@ static CharSequence unwrapValue(CharSequence cs) {
9796

9897
static long computeExpires(String expires) {
9998
if (expires != null) {
100-
Date expiresDate = DateParser.get().parse(expires, new ParsePosition(0));
99+
Date expiresDate = DateParser.parse(expires);
101100
if (expiresDate != null)
102101
return expiresDate.getTime();
103102
}

client/src/main/java/org/asynchttpclient/cookie/DateParser.java

Lines changed: 26 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -12,85 +12,50 @@
1212
*/
1313
package org.asynchttpclient.cookie;
1414

15-
import java.text.ParsePosition;
16-
import java.text.SimpleDateFormat;
15+
import java.time.LocalDateTime;
16+
import java.time.ZoneOffset;
17+
import java.time.ZonedDateTime;
18+
import java.time.format.DateTimeFormatter;
1719
import java.util.Date;
1820
import java.util.Locale;
19-
import java.util.TimeZone;
2021

2122
/**
2223
* A parser for <a href="http://tools.ietf.org/html/rfc2616#section-3.3">RFC2616
2324
* Date format</a>.
2425
*
2526
* @author slandelle
2627
*/
27-
@SuppressWarnings("serial")
28-
public class DateParser extends SimpleDateFormat {
29-
30-
private final SimpleDateFormat format1 = new RFC2616DateParserObsolete1();
31-
private final SimpleDateFormat format2 = new RFC2616DateParserObsolete2();
32-
33-
private static final ThreadLocal<DateParser> DATE_FORMAT_HOLDER = new ThreadLocal<DateParser>() {
34-
@Override
35-
protected DateParser initialValue() {
36-
return new DateParser();
28+
public final class DateParser {
29+
30+
private static final DateTimeFormatter PROPER_FORMAT_RFC822 = DateTimeFormatter.RFC_1123_DATE_TIME;
31+
// give up on pre 2000 dates
32+
private static final DateTimeFormatter OBSOLETE_FORMAT1_RFC850 = DateTimeFormatter.ofPattern("EEEE, dd-MMM-yy HH:mm:ss z", Locale.ENGLISH);
33+
private static final DateTimeFormatter OBSOLETE_FORMAT2_ANSIC = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss yyyy", Locale.ENGLISH);
34+
35+
private static Date parseZonedDateTimeSilent(String text, DateTimeFormatter formatter) {
36+
try {
37+
return Date.from(ZonedDateTime.parse(text, formatter).toInstant());
38+
} catch (Exception e) {
39+
return null;
3740
}
38-
};
39-
40-
public static DateParser get() {
41-
return DATE_FORMAT_HOLDER.get();
4241
}
4342

44-
/**
45-
* Standard date format
46-
* <br>
47-
* E, d MMM yyyy HH:mm:ss z
48-
* e.g. Sun, 06 Nov 1994 08:49:37 GMT
49-
*/
50-
private DateParser() {
51-
super("E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
52-
setTimeZone(TimeZone.getTimeZone("GMT"));
43+
private static Date parseDateTimeSilent(String text, DateTimeFormatter formatter) {
44+
try {
45+
return Date.from(LocalDateTime.parse(text, formatter).toInstant(ZoneOffset.UTC));
46+
} catch (Exception e) {
47+
return null;
48+
}
5349
}
5450

55-
@Override
56-
public Date parse(String text, ParsePosition pos) {
57-
Date date = super.parse(text, pos);
51+
public static Date parse(String text) {
52+
Date date = parseZonedDateTimeSilent(text, PROPER_FORMAT_RFC822);
5853
if (date == null) {
59-
date = format1.parse(text, pos);
54+
date = parseZonedDateTimeSilent(text, OBSOLETE_FORMAT1_RFC850);
6055
}
6156
if (date == null) {
62-
date = format2.parse(text, pos);
57+
date = parseDateTimeSilent(text, OBSOLETE_FORMAT2_ANSIC);
6358
}
6459
return date;
6560
}
66-
67-
/**
68-
* First obsolete format
69-
* <br>
70-
* E, d-MMM-y HH:mm:ss z
71-
* e.g. Sunday, 06-Nov-94 08:49:37 GMT
72-
*/
73-
private static final class RFC2616DateParserObsolete1 extends SimpleDateFormat {
74-
private static final long serialVersionUID = -3178072504225114298L;
75-
76-
RFC2616DateParserObsolete1() {
77-
super("E, dd-MMM-yy HH:mm:ss z", Locale.ENGLISH);
78-
setTimeZone(TimeZone.getTimeZone("GMT"));
79-
}
80-
}
81-
82-
/**
83-
* Second obsolete format
84-
* <br>
85-
* EEE, MMM d HH:mm:ss yyyy
86-
* e.g. Sun Nov 6 08:49:37 1994
87-
*/
88-
private static final class RFC2616DateParserObsolete2 extends SimpleDateFormat {
89-
private static final long serialVersionUID = 3010674519968303714L;
90-
91-
RFC2616DateParserObsolete2() {
92-
super("E MMM d HH:mm:ss yyyy", Locale.ENGLISH);
93-
setTimeZone(TimeZone.getTimeZone("GMT"));
94-
}
95-
}
9661
}

client/src/test/java/org/asynchttpclient/cookie/RFC2616DateParserTest.java renamed to client/src/test/java/org/asynchttpclient/cookie/DateParserTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
*
2929
* @author slandelle
3030
*/
31-
public class RFC2616DateParserTest {
31+
public class DateParserTest {
3232

3333
@Test(groups = "fast")
3434
public void testRFC822() throws ParseException {
35-
Date date = DateParser.get().parse("Sun, 06 Nov 1994 08:49:37 GMT");
35+
Date date = DateParser.parse("Sun, 06 Nov 1994 08:49:37 GMT");
3636
assertNotNull(date);
3737

3838
Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"));
@@ -48,7 +48,7 @@ public void testRFC822() throws ParseException {
4848

4949
@Test(groups = "fast")
5050
public void testRFC822SingleDigitDayOfMonth() throws ParseException {
51-
Date date = DateParser.get().parse("Sun, 6 Nov 1994 08:49:37 GMT");
51+
Date date = DateParser.parse("Sun, 6 Nov 1994 08:49:37 GMT");
5252
assertNotNull(date);
5353

5454
Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"));
@@ -64,7 +64,7 @@ public void testRFC822SingleDigitDayOfMonth() throws ParseException {
6464

6565
@Test(groups = "fast")
6666
public void testRFC822SingleDigitHour() throws ParseException {
67-
Date date = DateParser.get().parse("Sun, 6 Nov 1994 8:49:37 GMT");
67+
Date date = DateParser.parse("Sun, 6 Nov 1994 8:49:37 GMT");
6868
assertNotNull(date);
6969

7070
Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"));
@@ -80,23 +80,23 @@ public void testRFC822SingleDigitHour() throws ParseException {
8080

8181
@Test(groups = "fast")
8282
public void testRFC850() throws ParseException {
83-
Date date = DateParser.get().parse("Sunday, 06-Nov-94 08:49:37 GMT");
83+
Date date = DateParser.parse("Saturday, 06-Nov-94 08:49:37 GMT");
8484
assertNotNull(date);
85-
85+
8686
Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"));
8787
cal.setTime(date);
88-
assertEquals(cal.get(Calendar.DAY_OF_WEEK), Calendar.SUNDAY);
88+
assertEquals(cal.get(Calendar.DAY_OF_WEEK), Calendar.SATURDAY);
8989
assertEquals(cal.get(Calendar.DAY_OF_MONTH), 6);
9090
assertEquals(cal.get(Calendar.MONTH), Calendar.NOVEMBER);
91-
assertEquals(cal.get(Calendar.YEAR), 1994);
91+
assertEquals(cal.get(Calendar.YEAR), 2094);
9292
assertEquals(cal.get(Calendar.HOUR), 8);
9393
assertEquals(cal.get(Calendar.MINUTE), 49);
9494
assertEquals(cal.get(Calendar.SECOND), 37);
9595
}
9696

9797
@Test(groups = "fast")
9898
public void testANSIC() throws ParseException {
99-
Date date = DateParser.get().parse("Sun Nov 6 08:49:37 1994");
99+
Date date = DateParser.parse("Sun Nov 6 08:49:37 1994");
100100
assertNotNull(date);
101101

102102
Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"));

client/src/test/java/org/asynchttpclient/netty/NettyAsyncResponseTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,12 @@
2727
import org.asynchttpclient.cookie.Cookie;
2828
import org.testng.annotations.Test;
2929

30-
/**
31-
* @author Benjamin Hanzelmann
32-
*/
3330
public class NettyAsyncResponseTest {
3431

3532
@Test(groups = "standalone")
3633
public void testCookieParseExpires() {
37-
// e.g. "Sun, 06-Feb-2012 03:45:24 GMT";
38-
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd-MMM-yyyy HH:mm:ss z", Locale.US);
34+
// e.g. "Tue, 27 Oct 2015 12:54:24 GMT";
35+
SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
3936
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
4037

4138
Date date = new Date(System.currentTimeMillis() + 60000);

0 commit comments

Comments
 (0)