Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
994493d1
Commit
994493d1
authored
Apr 16, 2019
by
Greg Messner
Browse files
Removed use of JAXB DatatypeConverter (#327).
parent
202355e8
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/org/gitlab4j/api/utils/ISO8601.java
View file @
994493d1
...
@@ -2,28 +2,39 @@ package org.gitlab4j.api.utils;
...
@@ -2,28 +2,39 @@ package org.gitlab4j.api.utils;
import
java.text.ParseException
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.text.SimpleDateFormat
;
import
java.time.Instant
;
import
java.time.OffsetDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.time.format.DateTimeFormatterBuilder
;
import
java.time.temporal.ChronoField
;
import
java.util.Calendar
;
import
java.util.Calendar
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.Map
;
import
java.util.Map
;
import
java.util.TimeZone
;
import
java.util.TimeZone
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentHashMap
;
import
javax.xml.bind.DatatypeConverter
;
/**
/**
* This class provides utility methods for parsing and formatting ISO8601 formatted dates.
* This class provides utility methods for parsing and formatting ISO8601 formatted dates.
*/
*/
public
class
ISO8601
{
public
class
ISO8601
{
public
static
final
String
PATTERN
=
"yyyy-MM-dd'T'HH:mm:ssZ"
;
public
static
final
String
PATTERN
=
"yyyy-MM-dd'T'HH:mm:ssZ"
;
public
static
final
String
MSEC_PATTERN
=
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
;
public
static
final
String
SPACEY_PATTERN
=
"yyyy-MM-dd HH:mm:ss Z"
;
public
static
final
String
SPACEY_PATTERN
=
"yyyy-MM-dd HH:mm:ss Z"
;
public
static
final
String
SPACEY_MSEC_PATTERN
=
"yyyy-MM-dd HH:mm:ss.SSS Z"
;
public
static
final
String
PATTERN_MSEC
=
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
;
public
static
final
String
PATTERN_MSEC
=
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
;
public
static
final
String
OUTPUT_PATTERN
=
"yyyy-MM-dd'T'HH:mm:ss'Z'"
;
public
static
final
String
OUTPUT_PATTERN
=
"yyyy-MM-dd'T'HH:mm:ss'Z'"
;
public
static
final
String
OUTPUT_MSEC_PATTERN
=
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
;
public
static
final
String
OUTPUT_MSEC_PATTERN
=
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
;
public
static
final
String
UTC_PATTERN
=
"yyyy-MM-dd HH:mm:ss 'UTC'"
;
public
static
final
String
UTC_PATTERN
=
"yyyy-MM-dd HH:mm:ss 'UTC'"
;
private
static
final
String
PATTERN_REGEX
=
"\\d\\d\\d\\d-\\d\\d-\\d\\dT\\d\\d:\\d\\d:\\d\\d[-+]\\d\\d\\d\\d"
;
private
static
final
DateTimeFormatter
ODT_WITH_MSEC_PARSER
=
new
DateTimeFormatterBuilder
().
appendPattern
(
"yyyy-MM-dd[['T'][ ]HH:mm:ss.SSS[ ][XXXXX][XXXX]]"
).
toFormatter
();
private
static
final
String
SPACEY_PATTERN_REGEX
=
"\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d [-+]\\d\\d\\d\\d"
;
private
static
final
DateTimeFormatter
ODT_PARSER
=
new
DateTimeFormatterBuilder
().
appendPattern
(
"yyyy-MM-dd[['T'][ ]HH:mm:ss[.SSS][ ][X][XXX]]"
)
.
parseDefaulting
(
ChronoField
.
HOUR_OF_DAY
,
0
)
.
parseDefaulting
(
ChronoField
.
MINUTE_OF_HOUR
,
0
)
.
parseDefaulting
(
ChronoField
.
SECOND_OF_MINUTE
,
0
)
.
parseDefaulting
(
ChronoField
.
MILLI_OF_SECOND
,
0
)
.
parseDefaulting
(
ChronoField
.
OFFSET_SECONDS
,
0
)
.
toFormatter
();
// Set up ThreadLocal storage to save a thread local SimpleDateFormat keyed with the format string
// Set up ThreadLocal storage to save a thread local SimpleDateFormat keyed with the format string
private
static
final
class
SafeDateFormatter
{
private
static
final
class
SafeDateFormatter
{
...
@@ -116,39 +127,43 @@ public class ISO8601 {
...
@@ -116,39 +127,43 @@ public class ISO8601 {
}
}
/**
/**
* Parses an ISO8601 formatted string a returns a
Date
instance.
* Parses an ISO8601 formatted string a returns a
n Instant
instance.
*
*
* @param dateTimeString the ISO8601 formatted string
* @param dateTimeString the ISO8601 formatted string
* @return a
Date
instance for the ISO8601 formatted string
* @return a
n Instant
instance for the ISO8601 formatted string
* @throws ParseException if the provided string is not in the proper format
* @throws ParseException if the provided string is not in the proper format
*/
*/
public
static
Date
toDate
(
String
dateTimeString
)
throws
ParseException
{
public
static
Instant
toInstant
(
String
dateTimeString
)
throws
ParseException
{
if
(
dateTimeString
==
null
)
{
if
(
dateTimeString
==
null
)
{
return
(
null
);
return
(
null
);
}
}
dateTimeString
=
dateTimeString
.
trim
();
dateTimeString
=
dateTimeString
.
trim
();
if
(
dateTimeString
.
endsWith
(
"UTC"
))
{
if
(
dateTimeString
.
endsWith
(
"Z"
)
||
dateTimeString
.
endsWith
(
"UTC"
))
{
return
(
SafeDateFormatter
.
getDateFormat
(
UTC_PATTERN
)
.
parse
(
dateTimeString
));
return
(
Instant
.
parse
(
dateTimeString
));
}
else
{
}
else
{
try
{
Calendar
cal
=
DatatypeConverter
.
parseDateTime
(
dateTimeString
);
OffsetDateTime
odt
=
(
dateTimeString
.
length
()
>
25
?
return
(
cal
.
getTime
());
OffsetDateTime
.
parse
(
dateTimeString
,
ODT_WITH_MSEC_PARSER
)
:
}
catch
(
Exception
e
)
{
OffsetDateTime
.
parse
(
dateTimeString
,
ODT_PARSER
));
if
(
dateTimeString
.
matches
(
PATTERN_REGEX
))
{
// Try using the ISO8601 format
return
(
odt
.
toInstant
());
return
(
SafeDateFormatter
.
getDateFormat
(
PATTERN
).
parse
(
dateTimeString
));
}
else
if
(
dateTimeString
.
matches
(
SPACEY_PATTERN_REGEX
))
{
// Try using the invalid ISO8601 format with spaces, GitLab sometimes uses this
return
(
SafeDateFormatter
.
getDateFormat
(
SPACEY_PATTERN
).
parse
(
dateTimeString
));
}
else
{
throw
e
;
}
}
}
}
}
}
/**
* Parses an ISO8601 formatted string a returns a Date instance.
*
* @param dateTimeString the ISO8601 formatted string
* @return a Date instance for the ISO8601 formatted string
* @throws ParseException if the provided string is not in the proper format
*/
public
static
Date
toDate
(
String
dateTimeString
)
throws
ParseException
{
Instant
instant
=
toInstant
(
dateTimeString
);
return
(
instant
!=
null
?
Date
.
from
(
instant
)
:
null
);
}
/**
/**
* Parses an ISO8601 formatted string a returns a Calendar instance.
* Parses an ISO8601 formatted string a returns a Calendar instance.
*
*
...
@@ -159,6 +174,10 @@ public class ISO8601 {
...
@@ -159,6 +174,10 @@ public class ISO8601 {
public
static
Calendar
toCalendar
(
String
dateTimeString
)
throws
ParseException
{
public
static
Calendar
toCalendar
(
String
dateTimeString
)
throws
ParseException
{
Date
date
=
toDate
(
dateTimeString
);
Date
date
=
toDate
(
dateTimeString
);
if
(
date
==
null
)
{
return
(
null
);
}
Calendar
cal
=
Calendar
.
getInstance
();
Calendar
cal
=
Calendar
.
getInstance
();
cal
.
setTime
(
date
);
cal
.
setTime
(
date
);
return
(
cal
);
return
(
cal
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment