From 9e85169bdffd50ef729621ac4e296d54a0e61160 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 16 Feb 2022 10:14:05 +0000 Subject: [PATCH] improve performance of time zone lookup git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898124 13f79535-47bb-0310-9956-ffa450edef68 --- .../test/java/org/apache/poi/ooxml/TestPOIXMLProperties.java | 3 ++- poi/src/main/java/org/apache/poi/ss/usermodel/DateUtil.java | 5 +++-- poi/src/main/java/org/apache/poi/util/LocaleUtil.java | 3 ++- .../test/java/org/apache/poi/ss/usermodel/BaseTestCell.java | 3 ++- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/poi-ooxml/src/test/java/org/apache/poi/ooxml/TestPOIXMLProperties.java b/poi-ooxml/src/test/java/org/apache/poi/ooxml/TestPOIXMLProperties.java index b0c15b1340..926d840aff 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/ooxml/TestPOIXMLProperties.java +++ b/poi-ooxml/src/test/java/org/apache/poi/ooxml/TestPOIXMLProperties.java @@ -27,6 +27,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.ByteArrayInputStream; import java.io.IOException; import java.time.ZoneId; +import java.time.ZoneOffset; import java.util.Calendar; import java.util.Date; import java.util.Optional; @@ -329,7 +330,7 @@ public final class TestPOIXMLProperties { try (OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("sample.strict.xlsx"))) { POIXMLProperties props = new POIXMLProperties(pkg); assertNotNull(props.getCoreProperties().getCreated()); - assertEquals(2007, props.getCoreProperties().getCreated().toInstant().atZone(ZoneId.of("UTC")).getYear()); + assertEquals(2007, props.getCoreProperties().getCreated().toInstant().atZone(ZoneOffset.UTC).getYear()); } } diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/DateUtil.java b/poi/src/main/java/org/apache/poi/ss/usermodel/DateUtil.java index 1ad302c1a1..7a50951ac7 100644 --- a/poi/src/main/java/org/apache/poi/ss/usermodel/DateUtil.java +++ b/poi/src/main/java/org/apache/poi/ss/usermodel/DateUtil.java @@ -23,6 +23,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; +import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.ChronoField; @@ -92,7 +93,7 @@ public class DateUtil { */ public static LocalDateTime toLocalDateTime(Date date) { return date.toInstant() - .atZone(TimeZone.getTimeZone("UTC").toZoneId()) // java.util.Date uses UTC + .atZone(TimeZone.getTimeZone(ZoneOffset.UTC).toZoneId()) // java.util.Date uses UTC .toLocalDateTime(); } @@ -103,7 +104,7 @@ public class DateUtil { */ public static LocalDateTime toLocalDateTime(Calendar date) { return date.toInstant() - .atZone(TimeZone.getTimeZone("UTC").toZoneId()) // java.util.Date uses UTC + .atZone(TimeZone.getTimeZone(ZoneOffset.UTC).toZoneId()) // java.util.Date uses UTC .toLocalDateTime(); } diff --git a/poi/src/main/java/org/apache/poi/util/LocaleUtil.java b/poi/src/main/java/org/apache/poi/util/LocaleUtil.java index 860999c25e..5f714522eb 100644 --- a/poi/src/main/java/org/apache/poi/util/LocaleUtil.java +++ b/poi/src/main/java/org/apache/poi/util/LocaleUtil.java @@ -19,6 +19,7 @@ package org.apache.poi.util; import java.nio.charset.Charset; +import java.time.ZoneOffset; import java.util.Calendar; import java.util.Locale; import java.util.TimeZone; @@ -45,7 +46,7 @@ public final class LocaleUtil { * Excel doesn't store TimeZone information in the file, so if in doubt, * use UTC to perform calculations */ - public static final TimeZone TIMEZONE_UTC = TimeZone.getTimeZone("UTC"); + public static final TimeZone TIMEZONE_UTC = TimeZone.getTimeZone(ZoneOffset.UTC); /** * Default encoding for unknown byte encodings of native files diff --git a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestCell.java b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestCell.java index 944f46dd1c..55f0343511 100644 --- a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestCell.java +++ b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestCell.java @@ -33,6 +33,7 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.ZoneOffset; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -1093,7 +1094,7 @@ public abstract class BaseTestCell { cell.setCellValue(new Date()); assertEquals(CellType.FORMULA, cell.getCellType()); - cell.setCellValue(GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.ROOT)); + cell.setCellValue(GregorianCalendar.getInstance(TimeZone.getTimeZone(ZoneOffset.UTC), Locale.ROOT)); assertEquals(CellType.FORMULA, cell.getCellType()); cell.setCellValue(1.0); -- 2.39.5