From a49d4bdd0729467b700f5eccfd353149ad347440 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 1 Sep 2015 21:31:02 +0000 Subject: [PATCH] Fix some Forbidden APIs errors git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1700675 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/ss/format/CellDateFormatter.java | 13 +++++++++---- .../apache/poi/ss/formula/functions/WeekNum.java | 10 +++++++--- src/java/org/apache/poi/util/HexRead.java | 5 +---- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/java/org/apache/poi/ss/format/CellDateFormatter.java b/src/java/org/apache/poi/ss/format/CellDateFormatter.java index 30c6e2275d..0e6cfa15b4 100644 --- a/src/java/org/apache/poi/ss/format/CellDateFormatter.java +++ b/src/java/org/apache/poi/ss/format/CellDateFormatter.java @@ -24,14 +24,19 @@ import java.util.Calendar; import java.util.Date; import java.util.Formatter; import java.util.Locale; +import java.util.TimeZone; import java.util.regex.Matcher; /** * Formats a date value. - * - * @author Ken Arnold, Industrious Media LLC */ public class CellDateFormatter extends CellFormatter { + /** + * Excel doesn't store TimeZone information in the file, so if in doubt, + * use UTC to perform calculations + */ + private static final TimeZone DEFAULT_TIMEZONE = TimeZone.getTimeZone("UTC"); + private boolean amPmUpper; private boolean showM; private boolean showAmPm; @@ -45,7 +50,7 @@ public class CellDateFormatter extends CellFormatter { "mm/d/y"); static { - Calendar c = Calendar.getInstance(); + Calendar c = Calendar.getInstance(DEFAULT_TIMEZONE, Locale.ROOT); c.set(1904, 0, 1, 0, 0, 0); EXCEL_EPOCH_DATE = c.getTime(); EXCEL_EPOCH_TIME = c.getTimeInMillis(); @@ -183,7 +188,7 @@ public class CellDateFormatter extends CellFormatter { if (!doneMillis) { Date dateObj = (Date) value; int pos = toAppendTo.length(); - Formatter formatter = new Formatter(toAppendTo); + Formatter formatter = new Formatter(toAppendTo, Locale.ROOT); try { long msecs = dateObj.getTime() % 1000; formatter.format(LOCALE, sFmt, msecs / 1000.0); diff --git a/src/java/org/apache/poi/ss/formula/functions/WeekNum.java b/src/java/org/apache/poi/ss/formula/functions/WeekNum.java index 9cfd26fe5c..f27ee7e30a 100644 --- a/src/java/org/apache/poi/ss/formula/functions/WeekNum.java +++ b/src/java/org/apache/poi/ss/formula/functions/WeekNum.java @@ -24,6 +24,7 @@ import org.apache.poi.ss.usermodel.DateUtil; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.Locale; +import java.util.TimeZone; /** * Implementation for Excel WeekNum() function.

@@ -39,10 +40,13 @@ import java.util.Locale; * Return_type is a number that determines on which day the week begins. The default is 1. * 1 Week begins on Sunday. Weekdays are numbered 1 through 7. * 2 Week begins on Monday. Weekdays are numbered 1 through 7. - * - * @author cedric dot walter @ gmail dot com */ public class WeekNum extends Fixed2ArgFunction implements FreeRefFunction { + /** + * Excel doesn't store TimeZone information in the file, so if in doubt, + * use UTC to perform calculations + */ + private static final TimeZone DEFAULT_TIMEZONE = TimeZone.getTimeZone("UTC"); public static final FreeRefFunction instance = new WeekNum(); @@ -53,7 +57,7 @@ public class WeekNum extends Fixed2ArgFunction implements FreeRefFunction { } catch (EvaluationException e) { return ErrorEval.VALUE_INVALID; } - Calendar serialNumCalendar = new GregorianCalendar(Locale.ROOT); + Calendar serialNumCalendar = new GregorianCalendar(DEFAULT_TIMEZONE, Locale.ROOT); serialNumCalendar.setTime(DateUtil.getJavaDate(serialNum, false)); int returnType = 0; diff --git a/src/java/org/apache/poi/util/HexRead.java b/src/java/org/apache/poi/util/HexRead.java index 51b0ceeb2e..d8fdbbbb5c 100644 --- a/src/java/org/apache/poi/util/HexRead.java +++ b/src/java/org/apache/poi/util/HexRead.java @@ -24,9 +24,6 @@ import java.util.ArrayList; /** * Utilities to read hex from files. * TODO - move to test packages - * - * @author Marc Johnson - * @author Glen Stampoultzis (glens at apache.org) */ public class HexRead { @@ -174,7 +171,7 @@ public class HexRead static public byte[] readFromString(String data) { try { - return readData(new ByteArrayInputStream( data.getBytes() ), -1); + return readData(new ByteArrayInputStream( data.getBytes(StringUtil.UTF8) ), -1); } catch (IOException e) { throw new RuntimeException(e); }