Skip to contentMethod: toString()
1: /*
2: * Copyright (c) 2016, 2022 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Public License v. 2.0 which is available at
6: * http://www.eclipse.org/legal/epl-2.0,
7: * or the Eclipse Distribution License v. 1.0 which is available at
8: * http://www.eclipse.org/org/documents/edl-v10.php.
9: *
10: * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
11: */
12:
13: package org.eclipse.yasson.internal;
14:
15: import java.time.format.DateTimeFormatter;
16: import java.time.format.DateTimeFormatterBuilder;
17: import java.util.Locale;
18: import java.util.Objects;
19:
20: import jakarta.json.bind.annotation.JsonbDateFormat;
21:
22: import static java.time.temporal.ChronoField.HOUR_OF_DAY;
23: import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
24: import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
25:
26: /**
27: * Formatter wrapper for different types of dates.
28: */
29: public class JsonbDateFormatter {
30:
31: private static final JsonbDateFormatter DEFAULT = new JsonbDateFormatter(JsonbDateFormat.DEFAULT_FORMAT,
32: Locale.getDefault().toLanguageTag());
33:
34: /**
35: * Default I-JSON date time formatter.
36: */
37: public static final DateTimeFormatter IJSON_DATE_FORMATTER = new DateTimeFormatterBuilder()
38: .parseCaseInsensitive()
39: .append(DateTimeFormatter.ISO_LOCAL_DATE)
40: .appendLiteral('T')
41: .appendValue(HOUR_OF_DAY, 2)
42: .appendLiteral(':')
43: .appendValue(MINUTE_OF_HOUR, 2)
44: .appendLiteral(':')
45: .appendValue(SECOND_OF_MINUTE, 2)
46: .appendLiteral('Z')
47: .appendOffset("+HH:MM", "+00:00")
48: .toFormatter();
49:
50: private final DateTimeFormatter dateTimeFormatter;
51: private final String format;
52: private final String locale;
53:
54: /**
55: * Creates an instance with cached {@link DateTimeFormatter}, format and locale.
56: *
57: * @param dateTimeFormatter Reused time formatter.
58: * @param format Format in string.
59: * @param locale Locale in string.
60: */
61: public JsonbDateFormatter(DateTimeFormatter dateTimeFormatter, String format, String locale) {
62: this.dateTimeFormatter = dateTimeFormatter;
63: this.format = format;
64: this.locale = locale;
65: }
66:
67: /**
68: * Creates an instance with format string and locale.
69: * Formatter will be created on every formatting / parsing operation.
70: *
71: * @param format Formatter format.
72: * @param locale Locale in string.
73: */
74: public JsonbDateFormatter(String format, String locale) {
75: this.format = format;
76: this.locale = locale;
77: this.dateTimeFormatter = null;
78: }
79:
80: /**
81: * Creates an instance with cached instance of {@link DateTimeFormatter}.
82: *
83: * @return Formatter instance.
84: */
85: public DateTimeFormatter getDateTimeFormatter() {
86: return dateTimeFormatter;
87: }
88:
89: /**
90: * Format string to be used either by formatter.
91: * Needed for formatting {@link java.util.Date} with {@link java.text.SimpleDateFormat},
92: * which is not threadsafe.
93: *
94: * @return Format.
95: */
96: public String getFormat() {
97: return format;
98: }
99:
100: /**
101: * Locale to use with formatter.
102: *
103: * @return Locale.
104: */
105: public String getLocale() {
106: return locale;
107: }
108:
109: public static JsonbDateFormatter getDefault() {
110: return DEFAULT;
111: }
112:
113: public boolean isDefault() {
114: return JsonbDateFormat.DEFAULT_FORMAT.equals(format);
115: }
116:
117: @Override
118: public boolean equals(Object o) {
119: if (this == o) {
120: return true;
121: }
122: if (o == null || getClass() != o.getClass()) {
123: return false;
124: }
125: JsonbDateFormatter that = (JsonbDateFormatter) o;
126: return Objects.equals(format, that.format)
127: && Objects.equals(locale, that.locale)
128: && Objects.equals(dateTimeFormatter, that.dateTimeFormatter);
129: }
130:
131: @Override
132: public int hashCode() {
133: return Objects.hash(dateTimeFormatter, format, locale);
134: }
135:
136: @Override
137: public String toString() {
138: return "JsonbDateFormatter{"
139: + "dateTimeFormatter=" + dateTimeFormatter
140: + ", format='" + format + '\''
141: + ", locale='" + locale + '\''
142: + '}';
143: }
144: }