Skip to content

Commit 52b250c

Browse files
committed
Fix timezone parsing in DateTimeParser with Java 6
Fix OfficeDev#181 and OfficeDev#44 --- add dependency on Joda time. Conflicts: pom.xml
2 parents e3d06a1 + 78249df commit 52b250c

5 files changed

Lines changed: 50 additions & 31 deletions

File tree

.idea/libraries/Maven__joda_time_joda_time_2_7.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ews-java-api.iml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.6" level="project" />
1515
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.3.3" level="project" />
1616
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
17+
<orderEntry type="library" name="Maven: joda-time:joda-time:2.7" level="project" />
1718
<orderEntry type="library" name="Maven: jcifs:jcifs:1.3.17" level="project" />
1819
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.11" level="project" />
1920
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@
5555
<version>1.2</version>
5656
</dependency>
5757

58+
<dependency>
59+
<groupId>joda-time</groupId>
60+
<artifactId>joda-time</artifactId>
61+
<version>2.7</version>
62+
</dependency>
63+
5864
<dependency>
5965
<groupId>junit</groupId>
6066
<artifactId>junit</artifactId>

src/main/java/microsoft/exchange/webservices/data/util/DateTimeParser.java

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,28 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
1010

1111
package microsoft.exchange.webservices.data.util;
1212

13-
import java.text.ParseException;
14-
import java.text.SimpleDateFormat;
13+
import org.joda.time.format.DateTimeFormat;
14+
import org.joda.time.format.DateTimeFormatter;
15+
1516
import java.util.Date;
16-
import java.util.TimeZone;
1717

1818
public class DateTimeParser {
1919

20-
private final SimpleDateFormat[] dateTimeFormats = {
21-
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX"),
22-
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX"),
23-
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"),
24-
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"),
25-
new SimpleDateFormat("yyyy-MM-ddX"),
26-
new SimpleDateFormat("yyyy-MM-dd")
20+
private final DateTimeFormatter[] dateTimeFormats = {
21+
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ").withZoneUTC(),
22+
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").withZoneUTC(),
23+
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss").withZoneUTC(),
24+
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS").withZoneUTC(),
25+
DateTimeFormat.forPattern("yyyy-MM-ddZ").withZoneUTC(),
26+
DateTimeFormat.forPattern("yyyy-MM-dd").withZoneUTC()
2727
};
2828

29-
private final SimpleDateFormat[] dateFormats = {
30-
new SimpleDateFormat("yyyy-MM-ddX"),
31-
new SimpleDateFormat("yyyy-MM-dd")
29+
private final DateTimeFormatter[] dateFormats = {
30+
DateTimeFormat.forPattern("yyyy-MM-ddZ").withZoneUTC(),
31+
DateTimeFormat.forPattern("yyyy-MM-dd").withZoneUTC()
3232
};
3333

3434

35-
public DateTimeParser() {
36-
// Set default timezone of the formats to UTC, which will be used when the date string doesn't supply a
37-
// timezone itself.
38-
39-
for (SimpleDateFormat format : dateTimeFormats) {
40-
format.setTimeZone(TimeZone.getTimeZone("UTC"));
41-
}
42-
43-
for (SimpleDateFormat format : dateFormats) {
44-
format.setTimeZone(TimeZone.getTimeZone("UTC"));
45-
}
46-
}
47-
4835
/**
4936
* Converts a date time string to local date time.
5037
*
@@ -85,11 +72,11 @@ private Date parseInternal(String value, boolean dateOnly) {
8572
value = value.substring(0, value.length() - 1) + "Z";
8673
}
8774

88-
SimpleDateFormat[] formats = dateOnly ? dateFormats : dateTimeFormats;
89-
for (SimpleDateFormat format : formats) {
75+
DateTimeFormatter[] formats = dateOnly ? dateFormats : dateTimeFormats;
76+
for (DateTimeFormatter format : formats) {
9077
try {
91-
return format.parse(value);
92-
} catch (ParseException e) {
78+
return format.parseDateTime(value).toDate();
79+
} catch (IllegalArgumentException e) {
9380
// Ignore and try the next pattern.
9481
}
9582
}

src/test/java/microsoft/exchange/webservices/data/util/DateTimeParserTest.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
1010

1111
package microsoft.exchange.webservices.data.util;
1212

13-
import microsoft.exchange.webservices.data.util.DateTimeParser;
1413
import org.junit.Before;
1514
import org.junit.Test;
1615
import org.junit.runner.RunWith;
@@ -22,6 +21,7 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
2221
import java.util.TimeZone;
2322

2423
import static org.junit.Assert.assertEquals;
24+
import static org.junit.Assert.assertNull;
2525

2626
@RunWith(JUnit4.class)
2727
public class DateTimeParserTest {
@@ -37,6 +37,12 @@ public void setUp() {
3737

3838
// Tests for DateTimeParser.convertDateTimeStringToDate()
3939

40+
@Test
41+
public void testDateTimeEmpty() {
42+
assertNull(parser.convertDateTimeStringToDate(null));
43+
assertNull(parser.convertDateTimeStringToDate(""));
44+
}
45+
4046
@Test
4147
public void testDateTimeZulu() {
4248
String dateString = "2015-01-08T10:11:12Z";
@@ -147,6 +153,12 @@ public void testDateOnly() {
147153

148154
// Tests for DateTimeParser.convertDateStringToDate()
149155

156+
@Test
157+
public void testDateOnlyEmpty() {
158+
assertNull(parser.convertDateStringToDate(null));
159+
assertNull(parser.convertDateStringToDate(""));
160+
}
161+
150162
@Test
151163
public void testDateOnlyZulu() {
152164
String dateString = "2015-01-08Z";

0 commit comments

Comments
 (0)