Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Remove joda and switch to use java.time (requires java 8) #620

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<!-- Eliminates the file encoding warning. Of course, all of your files
should probably be UTF-8 nowadays. -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<javaLanguage.version>1.6</javaLanguage.version>
<javaLanguage.version>1.8</javaLanguage.version>
<javadoc.doclint.param/>

<!-- Dependencies [BUILD]: -->
Expand All @@ -86,8 +86,8 @@
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
<nexus-staging-maven-plugin.version>1.6.5</nexus-staging-maven-plugin.version>
<maven-source-plugin.version>2.4</maven-source-plugin.version>
<animal-sniffer-maven-plugin.version>1.14</animal-sniffer-maven-plugin.version>
<animal-sniffer-maven-plugin.signature.version>1.1</animal-sniffer-maven-plugin.signature.version>
<animal-sniffer-maven-plugin.version>1.15</animal-sniffer-maven-plugin.version>
<animal-sniffer-maven-plugin.signature.version>1.0</animal-sniffer-maven-plugin.signature.version>
<!-- Dependencies [REPORT]: -->
<maven-site-plugin.version>3.4</maven-site-plugin.version>
<maven-project-info-reports-plugin.version>2.8</maven-project-info-reports-plugin.version>
Expand All @@ -99,7 +99,6 @@
<httpclient.version>4.4.1</httpclient.version>
<httpcore.version>4.4.1</httpcore.version>
<commons-logging.version>1.2</commons-logging.version>
<joda-time.version>2.8</joda-time.version>
<commons-lang3.version>3.4</commons-lang3.version>
<commons-io.version>2.4</commons-io.version>
<!-- Dependencies [TEST]: -->
Expand Down Expand Up @@ -243,12 +242,6 @@
<version>${commons-lang3.version}</version>
</dependency>

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down Expand Up @@ -355,13 +348,13 @@
<configuration>
<signature>
<groupId>org.codehaus.mojo.signature</groupId>
<artifactId>java16-sun</artifactId>
<artifactId>java18</artifactId>
<version>${animal-sniffer-maven-plugin.signature.version}</version>
</signature>
</configuration>
<executions>
<execution>
<id>check-java16-sun</id>
<id>check-java18</id>
<phase>test</phase>
<goals>
<goal>check</goal>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.Period;
import org.joda.time.format.ISOPeriodFormat;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
Expand All @@ -70,6 +68,7 @@
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -883,9 +882,9 @@ public static TimeSpan getXSDurationToTimeSpan(String xsDuration) {
xsDuration = xsDuration.replace("-P", "P");
}

Period period = Period.parse(xsDuration, ISOPeriodFormat.standard());
long retval = period.toStandardDuration().getMillis();
Duration period = Duration.parse(xsDuration);

long retval = period.toMillis();

if (negative) {
retval = -retval;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,20 @@
package microsoft.exchange.webservices.data.util;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.util.Date;

import static java.time.temporal.ChronoField.INSTANT_SECONDS;

public final class DateTimeUtils {

private static final DateTimeFormatter[] DATE_TIME_FORMATS = createDateTimeFormats();
Expand Down Expand Up @@ -84,8 +93,29 @@ private static Date parseInternal(String value, boolean dateOnly) {
final DateTimeFormatter[] formats = dateOnly ? DATE_FORMATS : DATE_TIME_FORMATS;
for (final DateTimeFormatter format : formats) {
try {
return format.parseDateTime(value).toDate();
} catch (IllegalArgumentException e) {
TemporalAccessor temporalAccessor = format.parse(value);

Instant instant = null;

if (!temporalAccessor.isSupported(INSTANT_SECONDS)) {
// Only date
ZoneOffset zoneOffset = ZoneOffset.UTC;

if (temporalAccessor.isSupported(ChronoField.OFFSET_SECONDS)) {
// Have date with timezone offset
zoneOffset = ZoneOffset.ofTotalSeconds(temporalAccessor.get(ChronoField.OFFSET_SECONDS));
}

instant = LocalDate.from(temporalAccessor).atTime(OffsetTime.of(LocalTime.MIDNIGHT, zoneOffset)).toInstant();

} else {
// Date and time
instant = Instant.from(temporalAccessor);
}

return Date.from(instant);

} catch (DateTimeParseException e) {
// Ignore and try the next pattern.
}
}
Expand All @@ -97,21 +127,26 @@ private static Date parseInternal(String value, boolean dateOnly) {

private static DateTimeFormatter[] createDateTimeFormats() {
return new DateTimeFormatter[] {
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ").withZoneUTC(),
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").withZoneUTC(),
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ").withZoneUTC(),
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss").withZoneUTC(),
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS").withZoneUTC(),
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSS").withZoneUTC(),
DateTimeFormat.forPattern("yyyy-MM-ddZ").withZoneUTC(),
DateTimeFormat.forPattern("yyyy-MM-dd").withZoneUTC()
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZZZZZ"),
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"),
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"),
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"),
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ"),
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ"),
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss").withZone(ZoneOffset.UTC),
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS").withZone(ZoneOffset.UTC),
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSSS").withZone(ZoneOffset.UTC),
DateTimeFormatter.ofPattern("yyyy-MM-ddZZZZZ"),
DateTimeFormatter.ofPattern("yyyy-MM-ddZ"),
DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneOffset.UTC)
};
}

private static DateTimeFormatter[] createDateFormats() {
return new DateTimeFormatter[] {
DateTimeFormat.forPattern("yyyy-MM-ddZ").withZoneUTC(),
DateTimeFormat.forPattern("yyyy-MM-dd").withZoneUTC()
DateTimeFormatter.ofPattern("yyyy-MM-ddZZZZZ"),
DateTimeFormatter.ofPattern("yyyy-MM-ddZ"),
DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneOffset.UTC)
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@

package microsoft.exchange.webservices.data.core;

import microsoft.exchange.webservices.data.core.EwsUtilities;
import microsoft.exchange.webservices.data.misc.TimeSpan;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.time.format.DateTimeParseException;

@RunWith(JUnit4.class)
public class XSDurationTest {

Expand Down Expand Up @@ -62,7 +62,7 @@ public void testPeriodFail2() {
Assert.assertEquals("P0DT3H40M59.0S", EwsUtilities.getTimeSpanToXSDuration(timeSpan));
}

@Test(expected = IllegalArgumentException.class)
@Test(expected = DateTimeParseException.class)
public void testPeriodFail() {
TimeSpan timeSpan = EwsUtilities.getXSDurationToTimeSpan("P2H30M59.0S");
Assert.assertEquals("-P0DT2H30M59.0S", EwsUtilities.getTimeSpanToXSDuration(timeSpan));
Expand Down