Skip to contentMethod: XmlGregorianCalendarSerializer(TypeSerializerBuilder)
1: /*
2: * Copyright (c) 2018, 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.serializer.types;
14:
15: import java.time.Instant;
16: import java.time.ZonedDateTime;
17: import java.time.format.DateTimeFormatter;
18: import java.time.temporal.TemporalAccessor;
19: import java.util.Locale;
20:
21: import javax.xml.datatype.XMLGregorianCalendar;
22:
23: /**
24: * Serializer of the {@link XMLGregorianCalendar} type.
25: */
26: class XmlGregorianCalendarSerializer extends AbstractDateSerializer<XMLGregorianCalendar> {
27:
28: XmlGregorianCalendarSerializer(TypeSerializerBuilder serializerBuilder) {
29: super(serializerBuilder);
30: }
31:
32: @Override
33: protected Instant toInstant(XMLGregorianCalendar value) {
34: return Instant.ofEpochMilli(value.toGregorianCalendar().getTimeInMillis());
35: }
36:
37: @Override
38: protected String formatDefault(XMLGregorianCalendar value, Locale locale) {
39: DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
40: return formatter
41: .withLocale(locale)
42: .withZone(value.toGregorianCalendar().getTimeZone().toZoneId())
43: .format(toTemporalAccessor(value));
44: }
45:
46: @Override
47: protected TemporalAccessor toTemporalAccessor(XMLGregorianCalendar object) {
48: return ZonedDateTime.ofInstant(Instant.ofEpochMilli(object.toGregorianCalendar().getTimeInMillis()),
49: object.toGregorianCalendar().getTimeZone().toZoneId());
50: }
51:
52: }