blob: adf8ead92a38a4d0b9e893336f59bcf5ffd75166 [file] [log] [blame]
Simon Hunt3d1b0652015-05-05 17:27:24 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Simon Hunt3d1b0652015-05-05 17:27:24 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Simon Hunt3d1b0652015-05-05 17:27:24 -070015 */
16
17package org.onosproject.ui.table.cell;
18
Simon Hunt5939e652015-05-06 16:20:23 -070019
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070020import static java.time.temporal.ChronoField.CLOCK_HOUR_OF_AMPM;
21import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
22import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
23
24import java.time.Instant;
25import java.time.ZoneId;
26import java.time.format.DateTimeFormatter;
27import java.time.format.DateTimeFormatterBuilder;
28import java.time.temporal.ChronoField;
29import java.time.temporal.TemporalAccessor;
Simon Hunt5939e652015-05-06 16:20:23 -070030import java.util.Locale;
Simon Hunt3d1b0652015-05-05 17:27:24 -070031
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070032import org.joda.time.DateTimeZone;
33
Simon Hunt3d1b0652015-05-05 17:27:24 -070034/**
35 * Formats time values using {@link DateTimeFormatter}.
36 */
Simon Hunt66f75432015-05-06 14:07:03 -070037public final class TimeFormatter extends AbstractCellFormatter {
Simon Hunt3d1b0652015-05-05 17:27:24 -070038
Simon Hunt5939e652015-05-06 16:20:23 -070039 private DateTimeFormatter dtf;
Simon Hunt3d1b0652015-05-05 17:27:24 -070040
Simon Hunt5939e652015-05-06 16:20:23 -070041 // NOTE: Unlike other formatters in this package, this one is not
42 // implemented as a Singleton, because instances may be
43 // decorated with alternate locale and/or timezone.
Simon Hunt66f75432015-05-06 14:07:03 -070044
Simon Hunt5939e652015-05-06 16:20:23 -070045 /**
46 * Constructs a time formatter that uses the default locale and timezone.
47 */
48 public TimeFormatter() {
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070049 dtf = new DateTimeFormatterBuilder()
50 .appendValue(CLOCK_HOUR_OF_AMPM)
51 .appendLiteral(':')
52 .appendValue(MINUTE_OF_HOUR, 2)
53 .optionalStart()
54 .appendLiteral(':')
55 .appendValue(SECOND_OF_MINUTE, 2)
56 .appendLiteral(' ')
57 .appendText(ChronoField.AMPM_OF_DAY)
58 .optionalStart()
59 .appendLiteral(' ')
60 .appendOffset("+HH:MM", "+00:00")
61 .toFormatter();
Simon Hunt3d1b0652015-05-05 17:27:24 -070062 }
63
64 /**
Simon Hunt5939e652015-05-06 16:20:23 -070065 * Sets the locale to use for formatting the time.
66 *
67 * @param locale locale to use for formatting
68 * @return self, for chaining
Simon Hunt3d1b0652015-05-05 17:27:24 -070069 */
Simon Hunt5939e652015-05-06 16:20:23 -070070 public TimeFormatter withLocale(Locale locale) {
71 dtf = dtf.withLocale(locale);
72 return this;
73 }
74
75 /**
76 * Sets the time zone to use for formatting the time.
77 *
78 * @param zone time zone to use
79 * @return self, for chaining
80 */
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070081 public TimeFormatter withZone(ZoneId zone) {
Simon Hunt5939e652015-05-06 16:20:23 -070082 dtf = dtf.withZone(zone);
83 return this;
84 }
85
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -070086 /**
87 * Sets the time zone to use for formatting the time.
88 *
89 * @param zone time zone to use
90 * @return self, for chaining
91 *
92 * @deprecated in 1.12.0
93 */
94 @Deprecated
95 public TimeFormatter withZone(DateTimeZone zone) {
96 return withZone(zone.toTimeZone().toZoneId());
97 }
98
Simon Hunt5939e652015-05-06 16:20:23 -070099 @Override
100 protected String nonNullFormat(Object value) {
Yuta HIGUCHI0c47d532017-08-18 23:16:35 -0700101 if (value instanceof TemporalAccessor) {
102 return dtf.format((TemporalAccessor) value);
103 } else if (value instanceof org.joda.time.DateTime) {
104 return dtf.format(Instant.ofEpochMilli(((org.joda.time.DateTime) value).getMillis()));
105 }
106 // should never reach here
107 return String.valueOf(value);
Simon Hunt5939e652015-05-06 16:20:23 -0700108 }
109
Simon Hunt3d1b0652015-05-05 17:27:24 -0700110}