• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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
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 package com.android.tradefed.util;
17 
18 import java.text.SimpleDateFormat;
19 import java.util.Date;
20 import java.util.TimeZone;
21 import java.util.concurrent.TimeUnit;
22 
23 /**
24  * Contains time related utility methods.
25  */
26 public class TimeUtil {
27 
28     // only static methods, don't allow construction
TimeUtil()29     private TimeUtil() {
30     }
31 
32     /**
33      * Return a prettified version of the given elapsed time in milliseconds.
34      */
formatElapsedTime(long elapsedTimeMs)35     public static String formatElapsedTime(long elapsedTimeMs) {
36         if (elapsedTimeMs < 1000) {
37             return String.format("%d ms", elapsedTimeMs);
38         }
39         long seconds = TimeUnit.MILLISECONDS.toSeconds(elapsedTimeMs) % 60;
40         long minutes = TimeUnit.MILLISECONDS.toMinutes(elapsedTimeMs) % 60;
41         long hours = TimeUnit.MILLISECONDS.toHours(elapsedTimeMs);
42         StringBuilder time = new StringBuilder();
43         if (hours > 0) {
44             time.append(hours);
45             time.append("h ");
46         }
47         if (minutes > 0) {
48             time.append(minutes);
49             time.append("m ");
50         }
51         time.append(seconds);
52         time.append("s");
53 
54         return time.toString();
55     }
56 
57     /**
58      * Return a readable formatted version of the given epoch time.
59      *
60      * @param epochTime the epoch time in milliseconds
61      * @return a user readable string
62      */
formatTimeStamp(long epochTime)63     public static String formatTimeStamp(long epochTime) {
64         return formatTimeStamp(epochTime, null);
65     }
66 
67     /**
68      * Internal helper to print a time with the given date format, or a default format if none
69      * is provided.
70      */
formatTimeStamp(long epochTime, SimpleDateFormat format)71     private static String formatTimeStamp(long epochTime, SimpleDateFormat format) {
72         if (format == null) {
73             format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
74         }
75         return format.format(new Date(epochTime));
76     }
77 
78     /**
79      * Return a readable formatted version of the given epoch time in GMT time instead of the local
80      * timezone.
81      *
82      * @param epochTime the epoch time in milliseconds
83      * @return a user readable string
84      */
formatTimeStampGMT(long epochTime)85     public static String formatTimeStampGMT(long epochTime) {
86         SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
87         timeFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
88         return formatTimeStamp(epochTime, timeFormat);
89     }
90 }
91