blob: 44dc19404989f6ded0a1e77723b1d87ebbf5144d [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.
15 *
16 */
17
18package org.onosproject.ui.table.cell;
19
20import org.joda.time.DateTime;
Simon Hunt5939e652015-05-06 16:20:23 -070021import org.joda.time.DateTimeZone;
Simon Hunt3d1b0652015-05-05 17:27:24 -070022import org.joda.time.format.DateTimeFormat;
23import org.joda.time.format.DateTimeFormatter;
Simon Hunt5939e652015-05-06 16:20:23 -070024
25import java.util.Locale;
Simon Hunt3d1b0652015-05-05 17:27:24 -070026
27/**
28 * Formats time values using {@link DateTimeFormatter}.
29 */
Simon Hunt66f75432015-05-06 14:07:03 -070030public final class TimeFormatter extends AbstractCellFormatter {
Simon Hunt3d1b0652015-05-05 17:27:24 -070031
Simon Hunt5939e652015-05-06 16:20:23 -070032 private DateTimeFormatter dtf;
Simon Hunt3d1b0652015-05-05 17:27:24 -070033
Simon Hunt5939e652015-05-06 16:20:23 -070034 // NOTE: Unlike other formatters in this package, this one is not
35 // implemented as a Singleton, because instances may be
36 // decorated with alternate locale and/or timezone.
Simon Hunt66f75432015-05-06 14:07:03 -070037
Simon Hunt5939e652015-05-06 16:20:23 -070038 /**
39 * Constructs a time formatter that uses the default locale and timezone.
40 */
41 public TimeFormatter() {
42 dtf = DateTimeFormat.longTime();
Simon Hunt3d1b0652015-05-05 17:27:24 -070043 }
44
45 /**
Simon Hunt5939e652015-05-06 16:20:23 -070046 * Sets the locale to use for formatting the time.
47 *
48 * @param locale locale to use for formatting
49 * @return self, for chaining
Simon Hunt3d1b0652015-05-05 17:27:24 -070050 */
Simon Hunt5939e652015-05-06 16:20:23 -070051 public TimeFormatter withLocale(Locale locale) {
52 dtf = dtf.withLocale(locale);
53 return this;
54 }
55
56 /**
57 * Sets the time zone to use for formatting the time.
58 *
59 * @param zone time zone to use
60 * @return self, for chaining
61 */
62 public TimeFormatter withZone(DateTimeZone zone) {
63 dtf = dtf.withZone(zone);
64 return this;
65 }
66
67 @Override
68 protected String nonNullFormat(Object value) {
69 return dtf.print((DateTime) value);
70 }
71
Simon Hunt3d1b0652015-05-05 17:27:24 -070072}