1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you 5 * may not use this file except in compliance with the License. You may 6 * 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 13 * implied. See the License for the specific language governing 14 * permissions and limitations under the License. 15 */ 16 package com.android.vts.util; 17 18 import java.time.Instant; 19 import java.time.ZoneId; 20 import java.time.ZonedDateTime; 21 import java.time.format.DateTimeFormatter; 22 import java.util.concurrent.TimeUnit; 23 import java.util.logging.Logger; 24 25 /** TimeUtil, a helper class for formatting times. */ 26 public class TimeUtil { 27 protected static final Logger logger = Logger.getLogger(TimeUtil.class.getName()); 28 29 protected static final String DATE_FORMAT = "yyyy-MM-dd"; 30 protected static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss (z)"; 31 public static final ZoneId PT_ZONE = ZoneId.of("America/Los_Angeles"); 32 33 /** 34 * Create a date time string from the provided timestamp. 35 * 36 * @param timeMicroseconds The time in microseconds 37 * @return A formatted date time string. 38 */ 39 public static String getDateTimeString(long timeMicroseconds) { 40 long timeMillis = TimeUnit.MICROSECONDS.toMillis(timeMicroseconds); 41 ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timeMillis), PT_ZONE); 42 return DateTimeFormatter.ofPattern(DATE_TIME_FORMAT).format(zdt); 43 } 44 45 /** 46 * Create a date string from the provided timestamp. 47 * 48 * @param timeMicroseconds The time in microseconds 49 * @return A formatted date string. 50 */ 51 public static String getDateString(long timeMicroseconds) { 52 long timeMillis = TimeUnit.MICROSECONDS.toMillis(timeMicroseconds); 53 ZonedDateTime zdt = ZonedDateTime.ofInstant(Instant.ofEpochMilli(timeMillis), PT_ZONE); 54 return DateTimeFormatter.ofPattern(DATE_FORMAT).format(zdt); 55 } 56 } 57