diff --git a/serde/src/java/org/apache/hadoop/hive/serde2/io/TimestampWritable.java b/serde/src/java/org/apache/hadoop/hive/serde2/io/TimestampWritable.java index 6866d49..3623c79 100644 --- a/serde/src/java/org/apache/hadoop/hive/serde2/io/TimestampWritable.java +++ b/serde/src/java/org/apache/hadoop/hive/serde2/io/TimestampWritable.java @@ -21,8 +21,8 @@ import java.io.DataOutput; import java.io.IOException; import java.sql.Timestamp; -import java.text.DateFormat; -import java.text.SimpleDateFormat; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; import java.util.Date; import org.apache.hadoop.hive.common.type.HiveDecimal; @@ -66,13 +66,8 @@ public static final int BINARY_SORTABLE_LENGTH = 11; - private static final ThreadLocal threadLocalDateFormat = - new ThreadLocal() { - @Override - protected DateFormat initialValue() { - return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - } - }; + public static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") + .withZone(ZoneOffset.UTC); private Timestamp timestamp = new Timestamp(0); @@ -389,17 +384,11 @@ public String toString() { populateTimestamp(); } - String timestampString = timestamp.toString(); - if (timestampString.length() > 19) { - if (timestampString.length() == 21) { - if (timestampString.substring(19).compareTo(".0") == 0) { - return threadLocalDateFormat.get().format(timestamp); - } - } - return threadLocalDateFormat.get().format(timestamp) + timestampString.substring(19); + if (timestamp.getNanos() > 0) { + return timestamp.toString(); } - return threadLocalDateFormat.get().format(timestamp); + return timestamp.toLocalDateTime().format(DATE_TIME_FORMAT); } @Override