blob: 58c70930afdca22b0c84e9be215c849f941fe00c [file] [log] [blame]
Simon Hunt3d1b0652015-05-05 17:27:24 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
19import org.joda.time.DateTime;
Simon Hunt5939e652015-05-06 16:20:23 -070020import org.joda.time.DateTimeZone;
Simon Hunt3d1b0652015-05-05 17:27:24 -070021import org.joda.time.format.DateTimeFormat;
22import org.joda.time.format.DateTimeFormatter;
Simon Hunt5939e652015-05-06 16:20:23 -070023
24import java.util.Locale;
Simon Hunt3d1b0652015-05-05 17:27:24 -070025
26/**
27 * Formats time values using {@link DateTimeFormatter}.
28 */
Simon Hunt66f75432015-05-06 14:07:03 -070029public final class TimeFormatter extends AbstractCellFormatter {
Simon Hunt3d1b0652015-05-05 17:27:24 -070030
Simon Hunt5939e652015-05-06 16:20:23 -070031 private DateTimeFormatter dtf;
Simon Hunt3d1b0652015-05-05 17:27:24 -070032
Simon Hunt5939e652015-05-06 16:20:23 -070033 // NOTE: Unlike other formatters in this package, this one is not
34 // implemented as a Singleton, because instances may be
35 // decorated with alternate locale and/or timezone.
Simon Hunt66f75432015-05-06 14:07:03 -070036
Simon Hunt5939e652015-05-06 16:20:23 -070037 /**
38 * Constructs a time formatter that uses the default locale and timezone.
39 */
40 public TimeFormatter() {
41 dtf = DateTimeFormat.longTime();
Simon Hunt3d1b0652015-05-05 17:27:24 -070042 }
43
44 /**
Simon Hunt5939e652015-05-06 16:20:23 -070045 * Sets the locale to use for formatting the time.
46 *
47 * @param locale locale to use for formatting
48 * @return self, for chaining
Simon Hunt3d1b0652015-05-05 17:27:24 -070049 */
Simon Hunt5939e652015-05-06 16:20:23 -070050 public TimeFormatter withLocale(Locale locale) {
51 dtf = dtf.withLocale(locale);
52 return this;
53 }
54
55 /**
56 * Sets the time zone to use for formatting the time.
57 *
58 * @param zone time zone to use
59 * @return self, for chaining
60 */
61 public TimeFormatter withZone(DateTimeZone zone) {
62 dtf = dtf.withZone(zone);
63 return this;
64 }
65
66 @Override
67 protected String nonNullFormat(Object value) {
68 return dtf.print((DateTime) value);
69 }
70
Simon Hunt3d1b0652015-05-05 17:27:24 -070071}