6
6
import java .sql .Date ;
7
7
import java .sql .Time ;
8
8
import java .sql .Timestamp ;
9
- import java .text . DateFormat ;
10
- import java .text . ParseException ;
11
- import java .text . SimpleDateFormat ;
12
- import java .util . TimeZone ;
9
+ import java .time .* ;
10
+ import java .time . format . DateTimeFormatter ;
11
+ import java .time . format . DateTimeFormatterBuilder ;
12
+ import java .time . format . DateTimeParseException ;
13
13
14
14
import static java .nio .charset .StandardCharsets .UTF_8 ;
15
+ import static java .time .format .DateTimeFormatter .ISO_LOCAL_DATE ;
16
+ import static java .time .format .DateTimeFormatter .ISO_LOCAL_TIME ;
15
17
16
18
/**
17
19
* @author Antti Laisi
18
20
*
19
- * TODO: Add support for Java 8 temporal types and use new parsers .
21
+ * TODO: Add support for Java 8 temporal types.
20
22
*/
21
23
enum TemporalConversions {
22
24
;
25
+ static final DateTimeFormatter TIMESTAMP_FORMAT = new DateTimeFormatterBuilder ()
26
+ .parseCaseInsensitive ()
27
+ .append (ISO_LOCAL_DATE )
28
+ .appendLiteral (' ' )
29
+ .append (ISO_LOCAL_TIME )
30
+ .toFormatter ();
23
31
24
- static final ThreadLocal <DateFormat > DATE_FORMAT = new ThreadLocal <DateFormat >() {
25
- @ Override
26
- protected DateFormat initialValue () {
27
- return zoned (new SimpleDateFormat ("yyyy-MM-dd" ));
28
- }
29
- };
30
- static final ThreadLocal <DateFormat > TIME_FORMAT = new ThreadLocal <DateFormat >() {
31
- @ Override
32
- protected DateFormat initialValue () {
33
- return zoned (new SimpleDateFormat ("HH:mm:ss.SSS" ));
34
- }
35
- };
36
- static final ThreadLocal <DateFormat > TIME_FORMAT_NO_MILLIS = new ThreadLocal <DateFormat >() {
37
- @ Override
38
- protected DateFormat initialValue () {
39
- return zoned (new SimpleDateFormat ("HH:mm:ss" ));
40
- }
41
- };
42
- static final ThreadLocal <DateFormat > TIMESTAMP_FORMAT = new ThreadLocal <DateFormat >() {
43
- @ Override
44
- protected DateFormat initialValue () {
45
- return zoned (new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss.SSS" ));
46
- }
47
- };
32
+ static final DateTimeFormatter TIMESTAMPZ_FORMAT = new DateTimeFormatterBuilder ()
33
+ .parseCaseInsensitive ()
34
+ .append (ISO_LOCAL_DATE )
35
+ .appendLiteral (' ' )
36
+ .append (ISO_LOCAL_TIME )
37
+ .appendOffset ("+HH:mm" , "" )
38
+ .toFormatter ();
39
+
40
+ static final DateTimeFormatter TIMEZ_FORMAT = new DateTimeFormatterBuilder ()
41
+ .parseCaseInsensitive ()
42
+ .append (ISO_LOCAL_TIME )
43
+ .appendOffset ("+HH:mm" , "" )
44
+ .toFormatter ();
48
45
49
46
static Date toDate (Oid oid , byte [] value ) {
50
47
switch (oid ) {
51
48
case UNSPECIFIED : // fallthrough
52
49
case DATE :
53
50
String date = new String (value , UTF_8 );
54
51
try {
55
- return new Date ( DATE_FORMAT . get () .parse (date ). getTime ( ));
56
- } catch (ParseException e ) {
52
+ return Date . valueOf ( LocalDate .parse (date , ISO_LOCAL_DATE ));
53
+ } catch (DateTimeParseException e ) {
57
54
throw new SqlException ("Invalid date: " + date );
58
55
}
59
56
default :
@@ -62,52 +59,48 @@ static Date toDate(Oid oid, byte[] value) {
62
59
}
63
60
64
61
static Time toTime (Oid oid , byte [] value ) {
65
- switch ( oid ) {
66
- case UNSPECIFIED : // fallthrough
67
- case TIMETZ : // fallthrough
68
- case TIME :
69
- String time = new String ( value , UTF_8 );
70
- try {
71
- DateFormat format = time . length () == 8 ? TIME_FORMAT_NO_MILLIS . get () : TIME_FORMAT . get ();
72
- return new Time ( format .parse (time ). getTime ());
73
- } catch ( ParseException e ) {
74
- throw new SqlException ("Invalid time: " + time );
75
- }
76
- default :
77
- throw new SqlException ("Unsupported conversion " + oid . name () + " -> Time" );
62
+ String time = new String ( value , UTF_8 );
63
+ try {
64
+ switch ( oid ) {
65
+ case UNSPECIFIED : // fallthrough
66
+ case TIME :
67
+ return Time . valueOf ( LocalTime . parse ( time , ISO_LOCAL_TIME ));
68
+ case TIMETZ :
69
+ return Time . valueOf ( OffsetTime .parse (time , TIMEZ_FORMAT ). toLocalTime ());
70
+ default :
71
+ throw new SqlException ("Unsupported conversion " + oid . name () + " -> Time" );
72
+ }
73
+ } catch ( DateTimeParseException e ) {
74
+ throw new SqlException ("Invalid time: " + time );
78
75
}
79
76
}
80
77
81
78
static Timestamp toTimestamp (Oid oid , byte [] value ) {
82
- switch (oid ) {
83
- case UNSPECIFIED : // fallthrough
84
- case TIMESTAMP : // fallthrough
85
- case TIMESTAMPTZ :
86
- String time = new String (value , UTF_8 );
87
- try {
88
- return new Timestamp (TIMESTAMP_FORMAT .get ().parse (time ).getTime ());
89
- } catch (ParseException e ) {
90
- throw new SqlException ("Invalid time: " + time );
91
- }
92
- default :
93
- throw new SqlException ("Unsupported conversion " + oid .name () + " -> Time" );
79
+ String time = new String (value , UTF_8 );
80
+ try {
81
+ switch (oid ) {
82
+ case UNSPECIFIED : // fallthrough
83
+ case TIMESTAMP :
84
+ return Timestamp .valueOf (LocalDateTime .parse (time , TIMESTAMP_FORMAT ));
85
+ case TIMESTAMPTZ :
86
+ return Timestamp .valueOf (OffsetDateTime .parse (time , TIMESTAMPZ_FORMAT ).toLocalDateTime ());
87
+ default :
88
+ throw new SqlException ("Unsupported conversion " + oid .name () + " -> Time" );
89
+ }
90
+ } catch (DateTimeParseException e ) {
91
+ throw new SqlException ("Invalid time: " + time );
94
92
}
95
93
}
96
94
97
95
static byte [] fromTime (Time time ) {
98
- return TIME_FORMAT . get (). format (time ).getBytes (UTF_8 );
96
+ return ISO_LOCAL_TIME . format (time . toLocalTime () ).getBytes (UTF_8 );
99
97
}
100
98
101
99
static byte [] fromDate (Date date ) {
102
- return DATE_FORMAT . get (). format (date ).getBytes (UTF_8 );
100
+ return ISO_LOCAL_DATE . format (date . toLocalDate () ).getBytes (UTF_8 );
103
101
}
104
102
105
103
static byte [] fromTimestamp (Timestamp ts ) {
106
- return TIMESTAMP_FORMAT .get ().format (ts ).getBytes (UTF_8 );
107
- }
108
-
109
- private static SimpleDateFormat zoned (SimpleDateFormat format ) {
110
- format .setTimeZone (TimeZone .getTimeZone ("UTC" ));
111
- return format ;
104
+ return TIMESTAMP_FORMAT .format (ts .toLocalDateTime ()).getBytes (UTF_8 );
112
105
}
113
106
}
0 commit comments