summaryrefslogtreecommitdiff
path: root/src/main/java/com/keuin/kbackupfabric/util
diff options
context:
space:
mode:
authorKeuin <[email protected]>2021-01-24 20:01:17 +0800
committerkeuin <[email protected]>2021-01-24 20:01:17 +0800
commit5063ab12c48a6612d903c44c943230b6c323f602 (patch)
tree38ad6cf5f48377142a1d2fabfb1122eb9a681421 /src/main/java/com/keuin/kbackupfabric/util
parentdfd27f1a01a612c39bc17cac9793c80cc9b72e6b (diff)
Code refactor: eliminate unnecessary instance creation of IncrementalBackupFileNameEncoder
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util')
-rw-r--r--src/main/java/com/keuin/kbackupfabric/util/DateUtil.java31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/DateUtil.java b/src/main/java/com/keuin/kbackupfabric/util/DateUtil.java
new file mode 100644
index 0000000..b101cfd
--- /dev/null
+++ b/src/main/java/com/keuin/kbackupfabric/util/DateUtil.java
@@ -0,0 +1,31 @@
+package com.keuin.kbackupfabric.util;
+
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+
+public class DateUtil {
+ private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH-mm-ss");
+
+ public static String getString(LocalDateTime localDateTime) {
+ return localDateTime.format(formatter);
+ }
+
+ public static String getString(ZonedDateTime zonedDateTime) {
+ return zonedDateTime.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime().format(formatter);
+ }
+
+ @Deprecated
+ public static long toEpochSecond(String dateTimeString) {
+ ZoneId systemZone = ZoneId.systemDefault(); // my timezone
+ LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, formatter);
+ ZoneOffset currentOffsetForMyZone = systemZone.getRules().getOffset(localDateTime);
+ return localDateTime.toEpochSecond(currentOffsetForMyZone);
+ }
+
+ public static LocalDateTime toLocalDateTime(String timeString) {
+ return LocalDateTime.parse(timeString, formatter);
+ }
+}