forked from microsoftgraph/msgraph-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalendarSerializer.java
More file actions
142 lines (121 loc) · 5.49 KB
/
Copy pathCalendarSerializer.java
File metadata and controls
142 lines (121 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// ------------------------------------------------------------------------------
// Copyright (c) 2017 Microsoft Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ------------------------------------------------------------------------------
package com.microsoft.graph.serializer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
* Serializes and deserializes a string
*
* https://github.com/MSOpenTech/orc-for-android/blob/master/src/orc-android/
* src/main/java/com/microsoft/services/orc/serialization/impl/CalendarSerializer.java
*/
public final class CalendarSerializer {
/**
* Not available for instantiation
*/
private CalendarSerializer() {
}
/**
* Deserializes an ISO-8601 formatted date
*
* @param strVal the string value
* @return the calendar
* @throws java.text.ParseException the parse exception
*/
public static Calendar deserialize(final String strVal) throws ParseException {
// Change Z to adapt the string to a format
// that can be parsed in Java
final boolean hasZ = strVal.indexOf('Z') != -1;
final boolean hasDot = strVal.indexOf('.') != -1;
String modifiedStrVal;
final String zSuffix;
if (hasZ && hasDot) {
zSuffix = "";
modifiedStrVal = strVal.replace("Z", "+00:00");
} else if (hasZ && !hasDot) {
zSuffix = "Z";
modifiedStrVal = strVal.replace("Z", "+0000");
} else {
zSuffix = "";
modifiedStrVal = strVal;
}
final boolean hasOffset = modifiedStrVal.contains("T")
? (modifiedStrVal.substring(modifiedStrVal.indexOf('T') + 1).contains("+"))
|| (modifiedStrVal.substring(modifiedStrVal.indexOf('T') + 1).contains("-"))
: false;
// 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)
final String datePattern;
if (hasDot) {
String offsetSuffix = modifiedStrVal.substring(modifiedStrVal.indexOf(".") + 1);
// Find index of offset
int offsetIndex = -1;
if (hasOffset) {
offsetIndex = (offsetSuffix.indexOf('+') != -1) ? offsetSuffix.indexOf('+') : offsetSuffix.indexOf('-');
offsetIndex = modifiedStrVal.indexOf('.') + 1 + offsetIndex; //find offset index in original string
}
//SimpleDateFormat only supports 3 milliseconds
//Strip extra characters in millisecond field (eg: 2019-06-21T17:12:35.1385912-07:00)
String milliSeconds = hasOffset ? modifiedStrVal.substring(modifiedStrVal.indexOf('.') + 1, offsetIndex)
: modifiedStrVal.substring(modifiedStrVal.indexOf('.') + 1);
final int MILLIS_SEGMENT_LENGTH = 3;
if (milliSeconds.length() > MILLIS_SEGMENT_LENGTH) {
milliSeconds = milliSeconds.substring(0, MILLIS_SEGMENT_LENGTH);
}
//Handle date formats with and without offset cases
if (hasOffset) {
modifiedStrVal = modifiedStrVal.substring(0, modifiedStrVal.indexOf('.') + 1) + milliSeconds
+ modifiedStrVal.substring(offsetIndex);
datePattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
} else {
modifiedStrVal = modifiedStrVal.substring(0, modifiedStrVal.indexOf('.') + 1) + milliSeconds;
datePattern = "yyyy-MM-dd'T'HH:mm:ss.SSS" + zSuffix;
}
} else if (zSuffix != "") {
datePattern = "yyyy-MM-dd'T'HH:mm:ss" + zSuffix;
} else if (hasOffset){
datePattern = "yyyy-MM-dd'T'HH:mm:ssX";
} else {
datePattern = "yyyy-MM-dd'T'HH:mm:ss";
}
final SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
dateFormat.setTimeZone(TimeZone.getDefault());
final Date date = dateFormat.parse(modifiedStrVal);
final Calendar calendar = java.util.Calendar.getInstance();
calendar.setTime(date);
return calendar;
}
/**
* Serializes the string
*
* @param src the source calendar
* @return the string
*/
public static String serialize(final Calendar src) {
final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.'SSS'Z'", Locale.ROOT);
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
return dateFormat.format(src.getTime());
}
}