From 4af4af42f7fec3dc1060ec7b0eb9f51f871e63db Mon Sep 17 00:00:00 2001 From: lkd9125 Date: Thu, 14 Sep 2023 14:20:11 +0900 Subject: [PATCH] =?UTF-8?q?Socket,=20WebSocket=20Instant=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/palnet/comn/utils/DateUtils.java | 6 -- .../java/com/palnet/comn/utils/DateUtils.java | 69 ++++++++++++------- .../java/com/palnet/comn/utils/DateUtils.java | 67 +++++++++++------- .../process/scheduler/GpHistoryScheduler.java | 5 +- 4 files changed, 92 insertions(+), 55 deletions(-) diff --git a/pav-server/src/main/java/com/palnet/comn/utils/DateUtils.java b/pav-server/src/main/java/com/palnet/comn/utils/DateUtils.java index 3b6e03ac..ad272261 100644 --- a/pav-server/src/main/java/com/palnet/comn/utils/DateUtils.java +++ b/pav-server/src/main/java/com/palnet/comn/utils/DateUtils.java @@ -4,18 +4,12 @@ import lombok.extern.slf4j.Slf4j; import java.text.SimpleDateFormat; import java.time.Instant; -import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; -import java.util.ArrayList; -import java.util.Collections; import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; @Slf4j public class DateUtils { diff --git a/pav-socket/src/main/java/com/palnet/comn/utils/DateUtils.java b/pav-socket/src/main/java/com/palnet/comn/utils/DateUtils.java index 3a1f867a..942fb685 100644 --- a/pav-socket/src/main/java/com/palnet/comn/utils/DateUtils.java +++ b/pav-socket/src/main/java/com/palnet/comn/utils/DateUtils.java @@ -1,12 +1,17 @@ package com.palnet.comn.utils; import java.text.SimpleDateFormat; +import java.time.Instant; import java.time.LocalDateTime; +import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; import java.util.Date; +import lombok.extern.slf4j.Slf4j; + +@Slf4j public class DateUtils { public static String getCurrentTime(){ @@ -14,56 +19,71 @@ public class DateUtils { } public static String stringToFormat(String str) { - Date date = stringToDatetime(str); + Instant date = stringToDatetime(str); return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); } - public static Date stringToDatetime(String date) { + public static Instant stringToDatetime(String date) { SimpleDateFormat transFormat = new SimpleDateFormat("yyyyMMddHHmmss"); - Date result = null; + Date dateObject = null; try { if(date.length() == 14) { - result = transFormat.parse(date); + dateObject = transFormat.parse(date); } }catch(Exception e) { e.printStackTrace(); } + + Instant result = dateObject.toInstant(); return result; } - public static Date stringToDate(String date) { + public static Instant stringToDate(String date) { SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat transFormat2 = new SimpleDateFormat("yyyyMMdd"); - Date result = null; + Date dateObject = null; try { if(date.length() == 10) { - result = transFormat.parse(date); + dateObject = transFormat.parse(date); }else if(date.length() == 8) { - result = transFormat2.parse(date); + dateObject = transFormat2.parse(date); } }catch(Exception e) { e.printStackTrace(); } - + + Instant result = dateObject.toInstant(); + return result; } - public static LocalDateTime stringToLocalDateTime(String date) { - - DateTimeFormatter transFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); - LocalDateTime localDateTime = LocalDateTime.parse(date , transFormat); - - return localDateTime; + public static Instant stringToInstant(String date) { + + log.error("date -> {}", date); + + DateTimeFormatter formatter; + + if(date.length() == 10) formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + else formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + try { + LocalDateTime localDateTime = LocalDateTime.parse(date, formatter); + + return localDateTime.atZone(ZoneId.of("UTC")).toInstant(); + } catch (Exception e){ + e.printStackTrace(); + return null; + } } - public static LocalDateTime stringToLocalDate(String date) { + public static Instant stringToLocalDate(String date) { DateTimeFormatter transFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd"); DateTimeFormatter DATEFORMATTER = new DateTimeFormatterBuilder().append(transFormat) @@ -71,11 +91,10 @@ public class DateUtils { .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0) .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0) .toFormatter(); - - + LocalDateTime localDateTime = LocalDateTime.parse(date , DATEFORMATTER); - return localDateTime; + return localDateTime.atZone(ZoneId.of("UTC")).toInstant(); } @@ -92,14 +111,16 @@ public class DateUtils { } - public static long diffMinute(Date firstDate , Date secondDate) { - - long diffDate = secondDate.getTime() - firstDate.getTime(); - long diffTime = diffDate / (1000 * 60); + public static long diffMinute(Instant firstDate , Instant secondDate) { + long diffDate = secondDate.getEpochSecond() - firstDate.getEpochSecond(); + long diffTime = diffDate / (60); + return diffTime; } + + @@ -150,4 +171,4 @@ public class DateUtils { //// } // // } -} +} \ No newline at end of file diff --git a/pav-websocket/src/main/java/com/palnet/comn/utils/DateUtils.java b/pav-websocket/src/main/java/com/palnet/comn/utils/DateUtils.java index 3a1f867a..5b5082cd 100644 --- a/pav-websocket/src/main/java/com/palnet/comn/utils/DateUtils.java +++ b/pav-websocket/src/main/java/com/palnet/comn/utils/DateUtils.java @@ -1,12 +1,17 @@ package com.palnet.comn.utils; import java.text.SimpleDateFormat; +import java.time.Instant; import java.time.LocalDateTime; +import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; import java.util.Date; +import lombok.extern.slf4j.Slf4j; + +@Slf4j public class DateUtils { public static String getCurrentTime(){ @@ -14,56 +19,71 @@ public class DateUtils { } public static String stringToFormat(String str) { - Date date = stringToDatetime(str); + Instant date = stringToDatetime(str); return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date); } - public static Date stringToDatetime(String date) { + public static Instant stringToDatetime(String date) { SimpleDateFormat transFormat = new SimpleDateFormat("yyyyMMddHHmmss"); - Date result = null; + Date dateObject = null; try { if(date.length() == 14) { - result = transFormat.parse(date); + dateObject = transFormat.parse(date); } }catch(Exception e) { e.printStackTrace(); } + + Instant result = dateObject.toInstant(); return result; } - public static Date stringToDate(String date) { + public static Instant stringToDate(String date) { SimpleDateFormat transFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat transFormat2 = new SimpleDateFormat("yyyyMMdd"); - Date result = null; + Date dateObject = null; try { if(date.length() == 10) { - result = transFormat.parse(date); + dateObject = transFormat.parse(date); }else if(date.length() == 8) { - result = transFormat2.parse(date); + dateObject = transFormat2.parse(date); } }catch(Exception e) { e.printStackTrace(); } - + + Instant result = dateObject.toInstant(); + return result; } - public static LocalDateTime stringToLocalDateTime(String date) { - - DateTimeFormatter transFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); - LocalDateTime localDateTime = LocalDateTime.parse(date , transFormat); - - return localDateTime; + public static Instant stringToInstant(String date) { + + log.error("date -> {}", date); + + DateTimeFormatter formatter; + + if(date.length() == 10) formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + else formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + try { + LocalDateTime localDateTime = LocalDateTime.parse(date, formatter); + + return localDateTime.atZone(ZoneId.of("UTC")).toInstant(); + } catch (Exception e){ + e.printStackTrace(); + return null; + } } - public static LocalDateTime stringToLocalDate(String date) { + public static Instant stringToLocalDate(String date) { DateTimeFormatter transFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd"); DateTimeFormatter DATEFORMATTER = new DateTimeFormatterBuilder().append(transFormat) @@ -71,11 +91,10 @@ public class DateUtils { .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0) .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0) .toFormatter(); - - + LocalDateTime localDateTime = LocalDateTime.parse(date , DATEFORMATTER); - return localDateTime; + return localDateTime.atZone(ZoneId.of("UTC")).toInstant(); } @@ -92,14 +111,16 @@ public class DateUtils { } - public static long diffMinute(Date firstDate , Date secondDate) { - - long diffDate = secondDate.getTime() - firstDate.getTime(); - long diffTime = diffDate / (1000 * 60); + public static long diffMinute(Instant firstDate , Instant secondDate) { + long diffDate = secondDate.getEpochSecond() - firstDate.getEpochSecond(); + long diffTime = diffDate / (60); + return diffTime; } + + diff --git a/pav-websocket/src/main/java/com/palnet/process/scheduler/GpHistoryScheduler.java b/pav-websocket/src/main/java/com/palnet/process/scheduler/GpHistoryScheduler.java index b7837b1b..51957252 100644 --- a/pav-websocket/src/main/java/com/palnet/process/scheduler/GpHistoryScheduler.java +++ b/pav-websocket/src/main/java/com/palnet/process/scheduler/GpHistoryScheduler.java @@ -7,6 +7,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; +import java.time.Instant; import java.util.Date; import java.util.Map; import java.util.Objects; @@ -30,8 +31,8 @@ public class GpHistoryScheduler { // Key 의 존재하는 데이터는 마지막 서버수신 History Data if(Objects.nonNull(allHistory)) { allHistory.forEach((k, v) -> { - Date serverRcvDt = DateUtils.stringToDatetime(v.getServerRcvDt()); - long diffSecond = DateUtils.diffSecond(serverRcvDt, new Date()); + Instant serverRcvDt = DateUtils.stringToDatetime(v.getServerRcvDt()); + long diffSecond = DateUtils.diffMinute(serverRcvDt, Instant.now()); if(diffSecond > timeLimit) { gpHistoryShareContext.removeHistory(k);