fix: 修正时间从UTC到当前时区

main
huangfeng 7 months ago
parent f10be51ef6
commit 2320c11b8b

@ -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);
}

@ -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;
}
}

Loading…
Cancel
Save