@@ -15,9 +15,25 @@ public class ISO8601UtilsTest {
15
15
@ Rule
16
16
public final ExpectedException exception = ExpectedException .none ();
17
17
18
+ private static TimeZone utcTimeZone () {
19
+ return TimeZone .getTimeZone ("UTC" );
20
+ }
21
+
22
+ private static GregorianCalendar createUtcCalendar () {
23
+ TimeZone utc = utcTimeZone ();
24
+ GregorianCalendar calendar = new GregorianCalendar (utc );
25
+ // Calendar was created with current time, must clear it
26
+ calendar .clear ();
27
+ return calendar ;
28
+ }
29
+
18
30
@ Test
19
31
public void testDateFormatString () {
20
- Date date = new GregorianCalendar (2018 , Calendar .JUNE , 25 ).getTime ();
32
+ GregorianCalendar calendar = new GregorianCalendar (utcTimeZone (), Locale .US );
33
+ // Calendar was created with current time, must clear it
34
+ calendar .clear ();
35
+ calendar .set (2018 , Calendar .JUNE , 25 );
36
+ Date date = calendar .getTime ();
21
37
String dateStr = ISO8601Utils .format (date );
22
38
String expectedDate = "2018-06-25" ;
23
39
assertEquals (expectedDate , dateStr .substring (0 , expectedDate .length ()));
@@ -51,51 +67,28 @@ public void testDateParseWithDefaultTimezone() throws ParseException {
51
67
52
68
@ Test
53
69
public void testDateParseWithTimezone () throws ParseException {
54
- TimeZone defaultTimeZone = TimeZone .getDefault ();
55
- TimeZone .setDefault (TimeZone .getTimeZone ("UTC" ));
56
- Locale defaultLocale = Locale .getDefault ();
57
- Locale .setDefault (Locale .US );
58
- try {
59
- String dateStr = "2018-06-25T00:00:00-03:00" ;
60
- Date date = ISO8601Utils .parse (dateStr , new ParsePosition (0 ));
61
- Date expectedDate = new GregorianCalendar (2018 , Calendar .JUNE , 25 , 3 , 0 ).getTime ();
62
- assertEquals (expectedDate , date );
63
- } finally {
64
- TimeZone .setDefault (defaultTimeZone );
65
- Locale .setDefault (defaultLocale );
66
- }
70
+ String dateStr = "2018-06-25T00:00:00-03:00" ;
71
+ Date date = ISO8601Utils .parse (dateStr , new ParsePosition (0 ));
72
+ GregorianCalendar calendar = createUtcCalendar ();
73
+ calendar .set (2018 , Calendar .JUNE , 25 , 3 , 0 );
74
+ Date expectedDate = calendar .getTime ();
75
+ assertEquals (expectedDate , date );
67
76
}
68
77
69
78
@ Test
70
79
public void testDateParseSpecialTimezone () throws ParseException {
71
- TimeZone defaultTimeZone = TimeZone .getDefault ();
72
- TimeZone .setDefault (TimeZone .getTimeZone ("UTC" ));
73
- Locale defaultLocale = Locale .getDefault ();
74
- Locale .setDefault (Locale .US );
75
- try {
76
- String dateStr = "2018-06-25T00:02:00-02:58" ;
77
- Date date = ISO8601Utils .parse (dateStr , new ParsePosition (0 ));
78
- Date expectedDate = new GregorianCalendar (2018 , Calendar .JUNE , 25 , 3 , 0 ).getTime ();
79
- assertEquals (expectedDate , date );
80
- } finally {
81
- TimeZone .setDefault (defaultTimeZone );
82
- Locale .setDefault (defaultLocale );
83
- }
80
+ String dateStr = "2018-06-25T00:02:00-02:58" ;
81
+ Date date = ISO8601Utils .parse (dateStr , new ParsePosition (0 ));
82
+ GregorianCalendar calendar = createUtcCalendar ();
83
+ calendar .set (2018 , Calendar .JUNE , 25 , 3 , 0 );
84
+ Date expectedDate = calendar .getTime ();
85
+ assertEquals (expectedDate , date );
84
86
}
85
87
86
88
@ Test
87
89
public void testDateParseInvalidTime () throws ParseException {
88
- TimeZone defaultTimeZone = TimeZone .getDefault ();
89
- TimeZone .setDefault (TimeZone .getTimeZone ("UTC" ));
90
- Locale defaultLocale = Locale .getDefault ();
91
- Locale .setDefault (Locale .US );
92
- try {
93
- String dateStr = "2018-06-25T61:60:62-03:00" ;
94
- exception .expect (ParseException .class );
95
- ISO8601Utils .parse (dateStr , new ParsePosition (0 ));
96
- } finally {
97
- TimeZone .setDefault (defaultTimeZone );
98
- Locale .setDefault (defaultLocale );
99
- }
90
+ String dateStr = "2018-06-25T61:60:62-03:00" ;
91
+ exception .expect (ParseException .class );
92
+ ISO8601Utils .parse (dateStr , new ParsePosition (0 ));
100
93
}
101
94
}
0 commit comments