1 /*
2  * Copyright (C) 2020 The Android Open Source Project
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 distributed under the
11  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
12  * KIND, either express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package com.android.settings.display.darkmode;
17 
18 import android.content.Context;
19 
20 import java.time.LocalTime;
21 import java.util.Calendar;
22 import java.util.TimeZone;
23 
24 /**
25  * Formats LocalTime to the locale time string format
26 */
27 public class TimeFormatter {
28     private final Context mContext;
29     private final java.text.DateFormat mFormatter;
TimeFormatter(Context context)30     public TimeFormatter(Context context) {
31         mContext = context;
32         mFormatter = android.text.format.DateFormat.getTimeFormat(context);
33         mFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
34     }
35 
of(LocalTime time)36     public String of(LocalTime time) {
37         final Calendar c = Calendar.getInstance();
38         c.setTimeZone(mFormatter.getTimeZone());
39         c.set(Calendar.HOUR_OF_DAY, time.getHour());
40         c.set(Calendar.MINUTE, time.getMinute());
41         c.set(Calendar.SECOND, 0);
42         c.set(Calendar.MILLISECOND, 0);
43         return mFormatter.format(c.getTime());
44     }
45 
is24HourFormat()46     public boolean is24HourFormat() {
47         return android.text.format.DateFormat.is24HourFormat(mContext);
48     }
49 }
50