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

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1331477 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2012-04-27 15:49:49 +00:00
parent e030cf8159
commit b956b78e91
3 changed files with 70 additions and 9 deletions

View File

@ -34,6 +34,7 @@
<changes> <changes>
<release version="3.9-beta1" date="2012-??-??"> <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">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 &gt; 255</action> <action dev="poi-developers" type="fix">53101 - fixed evaluation of SUM over cell range &gt; 255</action>
<action dev="poi-developers" type="fix">49529 - avoid exception when cloning sheets with no drawing records and initialized drawing patriarch</action> <action dev="poi-developers" type="fix">49529 - avoid exception when cloning sheets with no drawing records and initialized drawing patriarch</action>

View File

@ -21,6 +21,7 @@ package org.apache.poi.ss.usermodel;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** /**
@ -124,6 +125,22 @@ public class DateUtil {
return value; return value;
} }
/**
* 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 * Given an Excel date with using 1900 date windowing, and
* converts it to a java.util.Date. * converts it to a java.util.Date.
@ -142,7 +159,38 @@ public class DateUtil {
* @see java.util.TimeZone * @see java.util.TimeZone
*/ */
public static Date getJavaDate(double date) { 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, * Given an Excel date with either 1900 or 1904 date windowing,
@ -164,14 +212,7 @@ public class DateUtil {
* @see java.util.TimeZone * @see java.util.TimeZone
*/ */
public static Date getJavaDate(double date, boolean use1904windowing) { public static Date getJavaDate(double date, boolean use1904windowing) {
if (!isValidExcelDate(date)) { return getJavaDate(date, use1904windowing, (TimeZone)null);
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();
} }
public static void setCalendar(Calendar calendar, int wholeDays, public static void setCalendar(Calendar calendar, int wholeDays,
int millisecondsInDay, boolean use1904windowing) { int millisecondsInDay, boolean use1904windowing) {

View File

@ -202,6 +202,25 @@ public final class TestHSSFDateUtil extends TestCase {
// Should match despite time-zone // Should match despite time-zone
assertEquals("Checking timezone " + id, expected.getTime(), javaDate.getTime()); 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);
} }
/** /**