Skip to content

Package: CalendarSerializer

CalendarSerializer

nameinstructionbranchcomplexitylinemethod
CalendarSerializer(TypeSerializerBuilder)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
formatDefault(Calendar, Locale)
M: 24 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
toInstant(Calendar)
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
toTemporalAccessor(Calendar)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 2021, 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.Calendar;
20: import java.util.Locale;
21:
22: /**
23: * Serializer of the {@link Calendar} type.
24: */
25: class CalendarSerializer extends AbstractDateSerializer<Calendar> {
26:
27: CalendarSerializer(TypeSerializerBuilder serializerBuilder) {
28: super(serializerBuilder);
29: }
30:
31: @Override
32: protected Instant toInstant(Calendar value) {
33: return value.toInstant();
34: }
35:
36: @Override
37: protected String formatDefault(Calendar value, Locale locale) {
38:• DateTimeFormatter formatter = value.isSet(Calendar.HOUR) || value.isSet(Calendar.HOUR_OF_DAY)
39: ? DateTimeFormatter.ISO_DATE_TIME
40: : DateTimeFormatter.ISO_DATE;
41: return formatter.withZone(value.getTimeZone().toZoneId())
42: .withLocale(locale).format(toTemporalAccessor(value));
43: }
44:
45: @Override
46: protected TemporalAccessor toTemporalAccessor(Calendar object) {
47: return ZonedDateTime.ofInstant(Instant.ofEpochMilli(object.getTimeInMillis()),
48: object.getTimeZone().toZoneId());
49: }
50:
51: }