Fix some Forbidden APIs errors

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1700645 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-09-01 19:11:20 +00:00
parent 47fb9df1a1
commit 71a62c0f9d
3 changed files with 20 additions and 10 deletions

View File

@ -20,6 +20,7 @@ package org.apache.poi.ss.formula.functions;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.EvaluationException;
@ -30,10 +31,13 @@ import org.apache.poi.ss.usermodel.DateUtil;
/**
* Implementation for the Excel function DATE
*
* @author Pavel Krupets (pkrupets at palmtreebusiness dot com)
*/
public final class DateFunc extends Fixed3ArgFunction {
/**
* 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 Function instance = new DateFunc();
@ -87,7 +91,7 @@ public final class DateFunc extends Fixed3ArgFunction {
}
// Turn this into a Java date
Calendar c = new GregorianCalendar(Locale.ROOT);
Calendar c = new GregorianCalendar(DEFAULT_TIMEZONE, Locale.ROOT);
c.set(year, month, day, 0, 0, 0);
c.set(Calendar.MILLISECOND, 0);

View File

@ -20,6 +20,7 @@ package org.apache.poi.ss.formula.functions;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.ValueEval;
@ -27,14 +28,16 @@ import org.apache.poi.ss.usermodel.DateUtil;
/**
* Implementation of Excel TODAY() Function<br/>
*
* @author Frank Taffelt
*/
public final class Today extends Fixed0ArgFunction {
/**
* 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 ValueEval evaluate(int srcRowIndex, int srcColumnIndex) {
Calendar now = new GregorianCalendar(Locale.ROOT);
Calendar now = new GregorianCalendar(DEFAULT_TIMEZONE, Locale.ROOT);
now.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DATE),0,0,0);
now.set(Calendar.MILLISECOND, 0);
return new NumberEval(DateUtil.getExcelDate(now.getTime()));

View File

@ -61,7 +61,10 @@ public class DateUtil {
// elapsed time patterns: [h],[m] and [s]
private static final Pattern date_ptrn4 = Pattern.compile("^\\[([hH]+|[mM]+|[sS]+)\\]");
// only get this static info once (because operations are not really cheap)
/**
* Excel doesn't store TimeZone information in the file, so if in doubt,
* use UTC to perform calculations
*/
private static final TimeZone TIMEZONE_UTC = TimeZone.getTimeZone("UTC");
@ -84,7 +87,7 @@ public class DateUtil {
* @param use1904windowing Should 1900 or 1904 date windowing be used?
*/
public static double getExcelDate(Date date, boolean use1904windowing) {
Calendar calStart = new GregorianCalendar(Locale.ROOT);
Calendar calStart = new GregorianCalendar(TIMEZONE_UTC, Locale.ROOT);
calStart.setTime(date); // If date includes hours, minutes, and seconds, set them to 0
return internalGetExcelDate(calStart, use1904windowing);
}
@ -322,7 +325,7 @@ public class DateUtil {
if (timeZone != null) {
calendar = new GregorianCalendar(timeZone, Locale.ROOT);
} else {
calendar = new GregorianCalendar(Locale.ROOT); // using default time-zone
calendar = new GregorianCalendar(TIMEZONE_UTC, Locale.ROOT); // using default time-zone
}
setCalendar(calendar, wholeDays, millisecondsInDay, use1904windowing, roundSeconds);
return calendar;