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
f05942ff
Commit
f05942ff
authored
Feb 07, 2017
by
Greg Messner
Browse files
Now handles all date strings received from GitLab.
parent
05678e63
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/main/java/com/messners/gitlab/api/ISO8601.java
View file @
f05942ff
...
...
@@ -6,6 +6,8 @@ import java.util.Calendar;
import
java.util.Date
;
import
java.util.TimeZone
;
import
javax.xml.bind.DatatypeConverter
;
/**
* This class provides utility methods for parsing and formatting ISO8601 formatted dates.
*/
...
...
@@ -14,29 +16,29 @@ public class ISO8601 {
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_MSEC_PATTERN
=
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
;
public
static
final
String
ALTERNATE
_PATTERN
=
"yyyy-MM-dd HH:mm:ss"
;
public
static
final
String
UTC
_PATTERN
=
"yyyy-MM-dd HH:mm:ss
'UTC'
"
;
private
static
final
SimpleDateFormat
iso8601Format
;
private
static
final
SimpleDateFormat
iso8601MsecFormat
;
private
static
final
SimpleDateFormat
iso8601OutputFormat
;
private
static
final
SimpleDateFormat
iso8601OutputMsecFormat
;
private
static
final
SimpleDateFormat
iso8601
Alternate
Format
;
private
static
final
SimpleDateFormat
iso8601
Utc
Format
;
static
{
iso8601Format
=
new
SimpleDateFormat
(
PATTERN
);
iso8601Format
.
setLenient
(
true
);
iso8601Format
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
GMT
"
));
iso8601Format
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
UTC
"
));
iso8601MsecFormat
=
new
SimpleDateFormat
(
PATTERN_MSEC
);
iso8601MsecFormat
.
setLenient
(
true
);
iso8601MsecFormat
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
GMT
"
));
iso8601MsecFormat
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
UTC
"
));
iso8601OutputFormat
=
new
SimpleDateFormat
(
OUTPUT_PATTERN
);
iso8601OutputFormat
.
setLenient
(
true
);
iso8601OutputFormat
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
GMT
"
));
iso8601OutputFormat
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
UTC
"
));
iso8601OutputMsecFormat
=
new
SimpleDateFormat
(
OUTPUT_MSEC_PATTERN
);
iso8601OutputMsecFormat
.
setLenient
(
true
);
iso8601OutputMsecFormat
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
GMT
"
));
iso8601
Alternate
Format
=
new
SimpleDateFormat
(
ALTERNATE
_PATTERN
);
iso8601
Alternate
Format
.
setLenient
(
true
);
iso8601
Alternate
Format
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
GMT
"
));
iso8601OutputMsecFormat
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
UTC
"
));
iso8601
Utc
Format
=
new
SimpleDateFormat
(
UTC
_PATTERN
);
iso8601
Utc
Format
.
setLenient
(
true
);
iso8601
Utc
Format
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
UTC
"
));
}
/**
...
...
@@ -47,7 +49,7 @@ public class ISO8601 {
public
static
String
getTimestamp
()
{
return
(
iso8601Format
.
format
(
new
Date
()));
}
/**
* Get a ISO8601formatted string for the current date and time.
*
...
...
@@ -85,9 +87,7 @@ public class ISO8601 {
}
long
time
=
date
.
getTime
();
return
(
time
%
1000
!=
0
?
iso8601OutputMsecFormat
.
format
(
date
)
:
iso8601OutputFormat
.
format
(
date
));
return
(
time
%
1000
!=
0
?
iso8601OutputMsecFormat
.
format
(
date
)
:
iso8601OutputFormat
.
format
(
date
));
}
/**
...
...
@@ -104,22 +104,13 @@ public class ISO8601 {
}
dateTimeString
=
dateTimeString
.
trim
();
SimpleDateFormat
fmt
;
if
(
dateTimeString
.
length
()
>
10
)
{
if
(
dateTimeString
.
charAt
(
10
)
==
'T'
)
{
fmt
=
(
dateTimeString
.
endsWith
(
"Z"
)
?
(
dateTimeString
.
charAt
(
dateTimeString
.
length
()
-
4
)
==
'.'
?
iso8601OutputMsecFormat
:
iso8601OutputFormat
)
:
iso8601Format
);
}
else
{
fmt
=
iso8601AlternateFormat
;
if
(
dateTimeString
.
endsWith
(
"UTC"
))
{
synchronized
(
iso8601UtcFormat
)
{
return
(
iso8601UtcFormat
.
parse
(
dateTimeString
));
}
}
else
{
fmt
=
iso8601Format
;
}
synchronized
(
fmt
)
{
return
(
fmt
.
parse
(
dateTimeString
));
Calendar
cal
=
DatatypeConverter
.
parseDateTime
(
dateTimeString
);
return
(
cal
.
getTime
());
}
}
...
...
src/main/java/com/messners/gitlab/api/JacksonJson.java
View file @
f05942ff
...
...
@@ -3,7 +3,10 @@ package com.messners.gitlab.api;
import
java.io.IOException
;
import
java.io.Reader
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.TimeZone
;
import
javax.ws.rs.Produces
;
import
javax.ws.rs.core.MediaType
;
...
...
@@ -14,8 +17,11 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
import
com.fasterxml.jackson.core.JsonGenerationException
;
import
com.fasterxml.jackson.core.JsonGenerator
;
import
com.fasterxml.jackson.core.JsonParseException
;
import
com.fasterxml.jackson.core.JsonParser
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.DeserializationContext
;
import
com.fasterxml.jackson.databind.DeserializationFeature
;
import
com.fasterxml.jackson.databind.JsonDeserializer
;
import
com.fasterxml.jackson.databind.JsonMappingException
;
import
com.fasterxml.jackson.databind.JsonSerializer
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
...
...
@@ -34,6 +40,13 @@ import com.messners.gitlab.api.models.AccessLevel;
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
class
JacksonJson
extends
JacksonJaxbJsonProvider
implements
ContextResolver
<
ObjectMapper
>
{
private
static
final
SimpleDateFormat
iso8601UtcFormat
;
static
{
iso8601UtcFormat
=
new
SimpleDateFormat
(
ISO8601
.
UTC_PATTERN
);
iso8601UtcFormat
.
setLenient
(
true
);
iso8601UtcFormat
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"UTC"
));
}
private
final
ObjectMapper
objectMapper
;
public
JacksonJson
()
{
...
...
@@ -50,6 +63,7 @@ public class JacksonJson extends JacksonJaxbJsonProvider implements ContextResol
SimpleModule
module
=
new
SimpleModule
(
"GitLabApiJsonModule"
);
module
.
addSerializer
(
Date
.
class
,
new
JsonDateSerializer
());
module
.
addDeserializer
(
Date
.
class
,
new
JsonDateDeserializer
());
module
.
addSerializer
(
AccessLevel
.
class
,
new
JsonAccessLevelSerializer
());
objectMapper
.
registerModule
(
module
);
}
...
...
@@ -137,7 +151,23 @@ public class JacksonJson extends JacksonJaxbJsonProvider implements ContextResol
gen
.
writeString
(
iso8601String
);
}
}
/**
* JsonDeserializer for deserializing ISO8601 formatted dates.
*/
public
static
class
JsonDateDeserializer
extends
JsonDeserializer
<
Date
>
{
@Override
public
Date
deserialize
(
JsonParser
jsonparser
,
DeserializationContext
context
)
throws
IOException
,
JsonProcessingException
{
try
{
return
(
ISO8601
.
toDate
(
jsonparser
.
getText
()));
}
catch
(
ParseException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
/**
* JsonSerializer for serializing AccessLevel values.
*/
...
...
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