From 9888fae559a319e08e8a255725f0d711789d020c Mon Sep 17 00:00:00 2001 From: machey Date: Thu, 28 Feb 2019 23:22:02 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=9F=E6=97=B6=E5=8C=BA=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../canal/client/adapter/support/Util.java | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/client-adapter/common/src/main/java/com/alibaba/otter/canal/client/adapter/support/Util.java b/client-adapter/common/src/main/java/com/alibaba/otter/canal/client/adapter/support/Util.java index ff97e4f7..cca38080 100644 --- a/client-adapter/common/src/main/java/com/alibaba/otter/canal/client/adapter/support/Util.java +++ b/client-adapter/common/src/main/java/com/alibaba/otter/canal/client/adapter/support/Util.java @@ -2,10 +2,7 @@ package com.alibaba.otter.canal.client.adapter.support; import java.io.File; import java.net.URL; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; +import java.sql.*; import java.time.*; import java.time.format.DateTimeFormatter; import java.util.Date; @@ -147,15 +144,22 @@ public class Util { return column; } - private static String UTC; // 当前时区 + private static String timeZone; // 当前时区 static { - TimeZone timeZone = TimeZone.getTimeZone(ZoneId.systemDefault().getId()); - int currentTimeZone = timeZone.getOffset(System.currentTimeMillis()) * 10 / (1000 * 3600); - int tmp = currentTimeZone % 10; - int m = 60 * tmp / 10; - UTC = "+" + String.format("%02d", currentTimeZone / 10) + ":" + String.format("%02d", m); - TimeZone.setDefault(TimeZone.getTimeZone("GMT" + UTC)); + TimeZone localTimeZone = TimeZone.getDefault(); + int rawOffset = localTimeZone.getRawOffset(); + String symbol = "+"; + if (rawOffset < 0) { + symbol = "-"; + } + rawOffset = Math.abs(rawOffset); + int offsetHore = rawOffset / 3600000; + int offsetMinute = rawOffset % 3600000 / 60000; + String hour = String.format("%1$02d", offsetHore); + String minute = String.format("%1$02d", offsetMinute); + timeZone = symbol + hour + ":" + minute; + TimeZone.setDefault(TimeZone.getTimeZone("GMT" + timeZone)); } /** @@ -177,12 +181,12 @@ public class Util { datetimeStr = "T" + datetimeStr; } - DateTime dateTime = new DateTime(datetimeStr, DateTimeZone.forID(UTC)); + DateTime dateTime = new DateTime(datetimeStr, DateTimeZone.forID(timeZone)); return dateTime.toDate(); } - public static LoadingCache dateFormatterCache = CacheBuilder.newBuilder() + private static LoadingCache dateFormatterCache = CacheBuilder.newBuilder() .build(new CacheLoader() { @Override