• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package ohos.devtools.datasources.utils.common.util;
17 
18 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager;
19 import org.apache.logging.log4j.LogManager;
20 import org.apache.logging.log4j.Logger;
21 
22 import java.time.Instant;
23 import java.time.LocalDateTime;
24 import java.time.ZoneOffset;
25 import java.time.format.DateTimeFormatter;
26 
27 /**
28  * Date and time utilities
29  *
30  * @since 2021/5/19 16:39
31  */
32 public final class DateTimeUtil {
33     private static final Logger LOGGER = LogManager.getLogger(DateTimeUtil.class);
34 
35     /**
36      * Format: yyyy-MM-dd HH:mm:ss
37      */
38     public static final String COMMON_PATTERN = "yyyy-MM-dd HH:mm:ss";
39 
40     /**
41      * Format: HH:mm
42      */
43     public static final String HOUR_MINUTES_PATTERN = "HH:mm";
44 
45     /**
46      * Format: HH:mm:ss
47      */
48     public static final String HOUR_MINUTES_SECONDS_PATTERN = "HH:mm:ss";
49 
50     private static final DateTimeFormatter COMMON_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
51 
52     private static final ZoneOffset DEFAULT_ZONE_OFFSET = ZoneOffset.of("+8");
53 
DateTimeUtil()54     private DateTimeUtil() {
55     }
56 
57     /**
58      * Convert the date to a string.
59      *
60      * @param dateTime Indicates the date and time to be converted.
61      * @return Returns a string converted from the date and time.
62      */
dateToString(LocalDateTime dateTime)63     public static String dateToString(LocalDateTime dateTime) {
64         if (ProfilerLogManager.isInfoEnabled()) {
65             LOGGER.info("dateToString");
66         }
67         assert dateTime != null;
68         return COMMON_FORMATTER.format(dateTime);
69     }
70 
71     /**
72      * Converts a string to date and time.
73      *
74      * @param dateStr Indicates the string to be converted to date and time.
75      * @return Returns the date and time converted from the given string.
76      */
stringToDate(String dateStr)77     public static LocalDateTime stringToDate(String dateStr) {
78         if (ProfilerLogManager.isInfoEnabled()) {
79             LOGGER.info("stringToDate");
80         }
81         assert dateStr != null;
82         return LocalDateTime.parse(dateStr, COMMON_FORMATTER);
83     }
84 
85     /**
86      * Gets the current date and time.
87      *
88      * @return Returns the curernt date and time.
89      */
getNowTime()90     public static LocalDateTime getNowTime() {
91         if (ProfilerLogManager.isInfoEnabled()) {
92             LOGGER.info("getNowTime");
93         }
94         return LocalDateTime.now();
95     }
96 
97     /**
98      * Converts the date and time to a string.
99      *
100      * @param dateTime Indicates the date and time to be converted.
101      * @param formatter Indicates the formatter used for formatting.
102      * @return Returns the date and time converted from the given string.
103      */
dateToString(LocalDateTime dateTime, DateTimeFormatter formatter)104     public static String dateToString(LocalDateTime dateTime, DateTimeFormatter formatter) {
105         if (ProfilerLogManager.isInfoEnabled()) {
106             LOGGER.info("dateToString");
107         }
108         assert dateTime != null;
109         return formatter.format(dateTime);
110     }
111 
112     /**
113      * Converts a string to date and time.
114      *
115      * @param dateStr Indicates the string to be converted to date and time.
116      * @param formatter Indicates the formatter used for formatting.
117      * @return Returns the date and time converted from the given string.
118      */
stringToDate(String dateStr, DateTimeFormatter formatter)119     public static LocalDateTime stringToDate(String dateStr, DateTimeFormatter formatter) {
120         if (ProfilerLogManager.isInfoEnabled()) {
121             LOGGER.info("stringToDate");
122         }
123         assert dateStr != null;
124         return LocalDateTime.parse(dateStr, formatter);
125     }
126 
127     /**
128      * Converts the date and time to Epoch time (in ms).
129      *
130      * @param dateTime Indicates the date and time to be converted.
131      * @return Returns a long string representing the Epoch time.
132      */
dateToTimeMillis(LocalDateTime dateTime)133     public static long dateToTimeMillis(LocalDateTime dateTime) {
134         if (ProfilerLogManager.isInfoEnabled()) {
135             LOGGER.info("dateToTimeMillis");
136         }
137         assert dateTime != null;
138         return dateTime.toInstant(DEFAULT_ZONE_OFFSET).toEpochMilli();
139     }
140 
141     /**
142      * Converts an Epoch time (in ms) to the date and time.
143      *
144      * @param timeMills Indicates the Epoch time (in ms) to be converted.
145      * @return Returns the date and time converted from the given Epoch time.
146      */
timeMillisToDate(long timeMills)147     public static LocalDateTime timeMillisToDate(long timeMills) {
148         if (ProfilerLogManager.isInfoEnabled()) {
149             LOGGER.info("timeMillisToDate");
150         }
151         Instant instant = Instant.ofEpochMilli(timeMills);
152         return LocalDateTime.ofInstant(instant, DEFAULT_ZONE_OFFSET);
153     }
154 
155     /**
156      * Gets the date and time in the given pattern.
157      *
158      * @param pattern Indicates the pattern of the date and time.
159      * @return Returns a string representing the date and time in the specified pattern.
160      */
getNowTimeString(String pattern)161     public static String getNowTimeString(String pattern) {
162         if (ProfilerLogManager.isInfoEnabled()) {
163             LOGGER.info("getNowTimeString");
164         }
165         LocalDateTime now = LocalDateTime.now();
166         DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
167         return now.format(formatter);
168     }
169 
170     /**
171      * Gets the timestamp (in ms).
172      *
173      * @return Returns a long string representing the timestamp.
174      */
getNowTimeLong()175     public static Long getNowTimeLong() {
176         LocalDateTime now = LocalDateTime.now();
177         return now.toInstant(DEFAULT_ZONE_OFFSET).toEpochMilli();
178     }
179 }
180