Skip to content

Commit 3847f9b

Browse files
author
Mohamed Bilal
committed
Updated CalendarSerializer to handle multiple offset formats
1 parent 2c6b9e2 commit 3847f9b

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

src/main/java/com/microsoft/graph/serializer/CalendarSerializer.java

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public final class CalendarSerializer {
4242
*/
4343
private CalendarSerializer() {
4444
}
45-
45+
4646
/**
4747
* Deserializes an ISO-8601 formatted date
4848
*
@@ -51,39 +51,64 @@ private CalendarSerializer() {
5151
* @throws java.text.ParseException the parse exception
5252
*/
5353
public static Calendar deserialize(final String strVal) throws ParseException {
54-
// Change Z to +0000 to adapt the string to a format
54+
// Change Z to adapt the string to a format
5555
// that can be parsed in Java
5656
final boolean hasZ = strVal.indexOf('Z') != -1;
57+
final boolean hasDot = strVal.indexOf('.') != -1;
58+
5759
String modifiedStrVal;
5860
final String zSuffix;
59-
if (hasZ) {
61+
if (hasZ && hasDot) {
62+
zSuffix = "";
63+
modifiedStrVal = strVal.replace("Z", "+00:00");
64+
} else if (hasZ && !hasDot) {
6065
zSuffix = "Z";
6166
modifiedStrVal = strVal.replace("Z", "+0000");
6267
} else {
6368
zSuffix = "";
6469
modifiedStrVal = strVal;
6570
}
66-
67-
// Parse the well-formatted date string.
71+
72+
// Parse the well-formatted date string with and without offsets (eg: 2019-06-21T17:12:35.138, 2019-06-21T17:12:35.138+0000, 2019-06-21T17:12:35.138-07:00)
6873
final String datePattern;
6974
if (modifiedStrVal.contains(".")) {
75+
76+
String offsetSuffix = modifiedStrVal.substring(modifiedStrVal.indexOf(".") + 1);
77+
final boolean hasOffset = offsetSuffix.contains("+") || offsetSuffix.contains("-");
78+
79+
// Find index of offset
80+
int offsetIndex = -1;
81+
if (hasOffset) {
82+
offsetIndex = (offsetSuffix.indexOf('+') != -1) ? offsetSuffix.indexOf('+') : offsetSuffix.indexOf('-');
83+
offsetIndex = modifiedStrVal.indexOf(".") + 1 + offsetIndex; //find offset index in original string
84+
}
85+
86+
7087
//SimpleDateFormat only supports 3 milliseconds
71-
String milliseconds = modifiedStrVal.substring(modifiedStrVal.indexOf('.') + 1,
72-
modifiedStrVal.indexOf('+'));
73-
final int millisSegmentLength = 3;
74-
if (milliseconds.length() > millisSegmentLength) {
75-
milliseconds = milliseconds.substring(0, millisSegmentLength);
76-
modifiedStrVal = modifiedStrVal.substring(0,
77-
modifiedStrVal.indexOf('.') + 1)
78-
+ milliseconds
79-
+ modifiedStrVal.substring(modifiedStrVal.indexOf('+'));
88+
//Strip extra characters in millisecond field (eg: 2019-06-21T17:12:35.1385912-07:00)
89+
String milliSeconds = hasOffset ? modifiedStrVal.substring(modifiedStrVal.indexOf('.') + 1, offsetIndex)
90+
: modifiedStrVal.substring(modifiedStrVal.indexOf('.') + 1);
91+
final int MILLIS_SEGMENT_LENGTH = 3;
92+
93+
if (milliSeconds.length() > MILLIS_SEGMENT_LENGTH) {
94+
milliSeconds = milliSeconds.substring(0, MILLIS_SEGMENT_LENGTH);
8095
}
81-
82-
datePattern = "yyyy-MM-dd'T'HH:mm:ss.SSS" + zSuffix;
96+
97+
//Handle date formats with and without offset cases
98+
if (hasOffset) {
99+
modifiedStrVal = modifiedStrVal.substring(0, modifiedStrVal.indexOf('.') + 1) + milliSeconds
100+
+ modifiedStrVal.substring(offsetIndex);
101+
datePattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
102+
} else {
103+
modifiedStrVal = modifiedStrVal.substring(0, modifiedStrVal.indexOf('.') + 1) + milliSeconds;
104+
datePattern = "yyyy-MM-dd'T'HH:mm:ss.SSS" + zSuffix;
105+
}
106+
83107
} else {
84108
datePattern = "yyyy-MM-dd'T'HH:mm:ss" + zSuffix;
85109
}
86110

111+
87112
final SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
88113
dateFormat.setTimeZone(TimeZone.getDefault());
89114

0 commit comments

Comments
 (0)