29
29
* Convert between a time and a String.
30
30
*/
31
31
public class UtcTimeOnlyConverter extends AbstractDateTimeConverter {
32
+
33
+ protected static final class Context {
34
+ private final DateFormat utcTimeFormat = createDateFormat ("HH:mm:ss" );
35
+ private final DateFormat utcTimeFormatMillis = createDateFormat ("HH:mm:ss.SSS" );
36
+ private final StringBuffer buffer = new StringBuffer (128 );
37
+ }
38
+
32
39
// SimpleDateFormats are not thread safe. A thread local is being
33
40
// used to maintain high concurrency among multiple session threads
34
- private static final ThreadLocal <UtcTimeOnlyConverter > utcTimeConverter = new ThreadLocal <UtcTimeOnlyConverter >();
35
- private final DateFormat utcTimeFormat = createDateFormat ("HH:mm:ss" );
36
- private final DateFormat utcTimeFormatMillis = createDateFormat ("HH:mm:ss.SSS" );
41
+ private static final ThreadLocal <Context > utcTimeConverter = new ThreadLocal <Context >() {
42
+ @ Override
43
+ protected Context initialValue () {
44
+ return new Context ();
45
+ }
46
+ };
37
47
38
48
/**
39
49
* Convert a time (represented as a Date) to a String (HH:MM:SS or HH:MM:SS.SSS)
@@ -43,15 +53,36 @@ public class UtcTimeOnlyConverter extends AbstractDateTimeConverter {
43
53
* @return a String representing the time.
44
54
*/
45
55
public static String convert (Date d , boolean includeMilliseconds ) {
46
- return getFormatter (includeMilliseconds ).format (d );
56
+ Context context = utcTimeConverter .get ();
57
+ try {
58
+ (includeMilliseconds ? context .utcTimeFormatMillis : context .utcTimeFormat )
59
+ .format (d , context .buffer , DontCareFieldPosition .INSTANCE );
60
+ return context .buffer .toString ();
61
+ } finally {
62
+ context .buffer .setLength (0 );
63
+ }
47
64
}
48
65
49
- private static DateFormat getFormatter (boolean includeMillis ) {
50
- UtcTimeOnlyConverter converter = utcTimeConverter .get ();
51
- if (converter == null ) {
52
- converter = new UtcTimeOnlyConverter ();
53
- utcTimeConverter .set (converter );
66
+ /**
67
+ * Convert a time (represented as a Date) to a String (HH:MM:SS or HH:MM:SS.SSS)
68
+ *
69
+ * @param d the date with the time to convert
70
+ * @param includeMilliseconds controls whether milliseconds are included in the result
71
+ * @param stringBuilder the out buffer to hold a String representing the time.
72
+ */
73
+ public static void convert (Date d , StringBuilder stringBuilder , boolean includeMilliseconds ) {
74
+ Context context = utcTimeConverter .get ();
75
+ try {
76
+ (includeMilliseconds ? context .utcTimeFormatMillis : context .utcTimeFormat )
77
+ .format (d , context .buffer , DontCareFieldPosition .INSTANCE );
78
+ stringBuilder .append (context .buffer );
79
+ } finally {
80
+ context .buffer .setLength (0 );
54
81
}
82
+ }
83
+
84
+ private static DateFormat getFormatter (boolean includeMillis ) {
85
+ Context converter = utcTimeConverter .get ();
55
86
return includeMillis ? converter .utcTimeFormatMillis : converter .utcTimeFormat ;
56
87
}
57
88
0 commit comments