You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2012/04/27 17:49:49 UTC
svn commit: r1331477 - in /poi/trunk/src:
documentation/content/xdocs/status.xml
java/org/apache/poi/ss/usermodel/DateUtil.java
testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java
Author: nick
Date: Fri Apr 27 15:49:49 2012
New Revision: 1331477
URL: http://svn.apache.org/viewvc?rev=1331477&view=rev
Log:
Fix bug #53092 - allow specifying of a TimeZone to DateUtil.getJavaDate(), for when it is known that a file comes from a different (known) timezone to the current machine
Modified:
poi/trunk/src/documentation/content/xdocs/status.xml
poi/trunk/src/java/org/apache/poi/ss/usermodel/DateUtil.java
poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java
Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=1331477&r1=1331476&r2=1331477&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Fri Apr 27 15:49:49 2012
@@ -34,6 +34,7 @@
<changes>
<release version="3.9-beta1" date="2012-??-??">
+ <action dev="poi-developers" type="fix">53092 - allow specifying of a TimeZone to DateUtil.getJavaDate(), for when it is known that a file comes from a different (known) timezone to the current machine</action>
<action dev="poi-developers" type="fix">53043 - don't duplicate hyperlink relationships when saving XSSF file</action>
<action dev="poi-developers" type="fix">53101 - fixed evaluation of SUM over cell range > 255</action>
<action dev="poi-developers" type="fix">49529 - avoid exception when cloning sheets with no drawing records and initialized drawing patriarch</action>
Modified: poi/trunk/src/java/org/apache/poi/ss/usermodel/DateUtil.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/DateUtil.java?rev=1331477&r1=1331476&r2=1331477&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/DateUtil.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/DateUtil.java Fri Apr 27 15:49:49 2012
@@ -21,6 +21,7 @@ package org.apache.poi.ss.usermodel;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
+import java.util.TimeZone;
import java.util.regex.Pattern;
/**
@@ -126,6 +127,22 @@ public class DateUtil {
/**
* Given an Excel date with using 1900 date windowing, and
+ * converts it to a java.util.Date.
+ *
+ * Excel Dates and Times are stored without any timezone
+ * information. If you know (through other means) that your file
+ * uses a different TimeZone to the system default, you can use
+ * this version of the getJavaDate() method to handle it.
+ *
+ * @param date The Excel date.
+ * @param tz The TimeZone to evaluate the date in
+ * @return Java representation of the date, or null if date is not a valid Excel date
+ */
+ public static Date getJavaDate(double date, TimeZone tz) {
+ return getJavaDate(date, false, tz);
+ }
+ /**
+ * Given an Excel date with using 1900 date windowing, and
* converts it to a java.util.Date.
*
* NOTE: If the default <code>TimeZone</code> in Java uses Daylight
@@ -142,7 +159,38 @@ public class DateUtil {
* @see java.util.TimeZone
*/
public static Date getJavaDate(double date) {
- return getJavaDate(date, false);
+ return getJavaDate(date, (TimeZone)null);
+ }
+
+ /**
+ * Given an Excel date with either 1900 or 1904 date windowing,
+ * converts it to a java.util.Date.
+ *
+ * Excel Dates and Times are stored without any timezone
+ * information. If you know (through other means) that your file
+ * uses a different TimeZone to the system default, you can use
+ * this version of the getJavaDate() method to handle it.
+ *
+ * @param date The Excel date.
+ * @param tz The TimeZone to evaluate the date in
+ * @param use1904windowing true if date uses 1904 windowing,
+ * or false if using 1900 date windowing.
+ * @return Java representation of the date, or null if date is not a valid Excel date
+ */
+ public static Date getJavaDate(double date, boolean use1904windowing, TimeZone tz) {
+ if (!isValidExcelDate(date)) {
+ return null;
+ }
+ Calendar calendar;
+ if (tz != null)
+ calendar = new GregorianCalendar(tz);
+ else
+ calendar = new GregorianCalendar(); // using default time-zone
+
+ int wholeDays = (int)Math.floor(date);
+ int millisecondsInDay = (int)((date - wholeDays) * DAY_MILLISECONDS + 0.5);
+ setCalendar(calendar, wholeDays, millisecondsInDay, use1904windowing);
+ return calendar.getTime();
}
/**
* Given an Excel date with either 1900 or 1904 date windowing,
@@ -164,14 +212,7 @@ public class DateUtil {
* @see java.util.TimeZone
*/
public static Date getJavaDate(double date, boolean use1904windowing) {
- if (!isValidExcelDate(date)) {
- return null;
- }
- int wholeDays = (int)Math.floor(date);
- int millisecondsInDay = (int)((date - wholeDays) * DAY_MILLISECONDS + 0.5);
- Calendar calendar = new GregorianCalendar(); // using default time-zone
- setCalendar(calendar, wholeDays, millisecondsInDay, use1904windowing);
- return calendar.getTime();
+ return getJavaDate(date, use1904windowing, (TimeZone)null);
}
public static void setCalendar(Calendar calendar, int wholeDays,
int millisecondsInDay, boolean use1904windowing) {
Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java?rev=1331477&r1=1331476&r2=1331477&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDateUtil.java Fri Apr 27 15:49:49 2012
@@ -202,6 +202,25 @@ public final class TestHSSFDateUtil exte
// Should match despite time-zone
assertEquals("Checking timezone " + id, expected.getTime(), javaDate.getTime());
}
+
+ // Check that the timezone aware getter works correctly
+ TimeZone cet = TimeZone.getTimeZone("Europe/Copenhagen");
+ TimeZone ldn = TimeZone.getTimeZone("Europe/London");
+ TimeZone.setDefault(cet);
+
+ // 12:45 on 27th April 2012
+ double excelDate = 41026.53125;
+
+ // Same, no change
+ assertEquals(
+ HSSFDateUtil.getJavaDate(excelDate, false).getTime(),
+ HSSFDateUtil.getJavaDate(excelDate, false, cet).getTime()
+ );
+
+ // London vs Copenhagen, should differ by an hour
+ Date cetDate = HSSFDateUtil.getJavaDate(excelDate, false);
+ Date ldnDate = HSSFDateUtil.getJavaDate(excelDate, false, ldn);
+ assertEquals(ldnDate.getTime() - cetDate.getTime(), 60*60*1000);
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org