diff --git a/src/main/java/com/xydl/cac/iec/IEDCollectService.java b/src/main/java/com/xydl/cac/iec/IEDCollectService.java index 3e59947..9edaf11 100644 --- a/src/main/java/com/xydl/cac/iec/IEDCollectService.java +++ b/src/main/java/com/xydl/cac/iec/IEDCollectService.java @@ -117,10 +117,7 @@ public class IEDCollectService { String value = iecClient.getValue(paramindexNew, fc); String time = iecClient.getValue(paramindexT, fc); log.info("采集到" + fc + " " + paramindexNew + "=" + value + ", t=" + time); - time = time.replace("T", " ").replace("Z", "").replace("z", ""); - Date date = DateUtil.parse(time); - date = DateUtil.addHour(date, 8); - time = DateUtil.format(date); + time = DateUtil.fromZoneUTCToLocal(time); _dataService.insertData(rpt.getTablename(), rpt.getEqmid(), time, rpt.getColname(), value); // 更新最新数据缓存 @@ -216,10 +213,7 @@ public class IEDCollectService { String colname = str[1]; value = bda.getValueString(); if ("acquisitionTime".equals(colname)) { - value = value.replace("T", " ").replace("Z", "").replace("z", ""); - Date date = DateUtil.parse(value); - date = DateUtil.addHour(date, 8); - value = DateUtil.format(date); + value = DateUtil.fromZoneUTCToLocal(value); } updateLastData(eqmid, colname, value, null); } diff --git a/src/main/java/com/xydl/cac/util/DateUtil.java b/src/main/java/com/xydl/cac/util/DateUtil.java index 1e7338f..3922930 100644 --- a/src/main/java/com/xydl/cac/util/DateUtil.java +++ b/src/main/java/com/xydl/cac/util/DateUtil.java @@ -2,14 +2,15 @@ package com.xydl.cac.util; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.time.LocalDate; -import java.time.ZoneId; +import java.time.*; +import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.Calendar; import java.util.Date; public class DateUtil { public final static String defaultDatePattern = "yyyy-MM-dd HH:mm:ss"; + public static DateTimeFormatter defaultFormatter = DateTimeFormatter.ofPattern(defaultDatePattern); /** * 获得默认的 date pattern @@ -102,4 +103,16 @@ public class DateUtil { return ChronoUnit.DAYS.between(startLocalDate, endLocalDate); } + public static String fromZoneUTCToLocal(String str) throws ParseException { + String time = str.replace("T", " ") + .replace("Z", "").replace("z", ""); + Date date = parse(time); + time = format(date); + LocalDateTime localtime = LocalDateTime.parse(time, defaultFormatter); + ZonedDateTime zonedDateTime = localtime.atZone(ZoneOffset.UTC); + ZonedDateTime convertedDateTime = zonedDateTime.withZoneSameInstant(ZoneId.systemDefault()); + String result = convertedDateTime.format(defaultFormatter); + return result; + } + }