Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Poi default timezone

Tags:

apache-poi

I am using Apache POI to evaluate every formula cell of a workbook. When a cell contains a call to the standard excel function NOW(), Poi evalutes it correctly and replaces the call with the current time - formatted in the default timezone of the VM. I would like to adjust this timezone. Is there any way to change the timezone of DateUtil?

I found the code of the NOW Implementation in 3.14-beta1, I attach it for reference:

/**
 * Implementation of Excel NOW() Function
 *
 * @author Frank Taffelt
 */
 public final class Now extends Fixed0ArgFunction {
     public ValueEval evaluate(int srcRowIndex, int srcColumnIndex) {
        Date now = new Date(System.currentTimeMillis());
        return new NumberEval(DateUtil.getExcelDate(now));
    }
}
like image 345
Mathis Avatar asked May 31 '26 12:05

Mathis


1 Answers

For the sake of completeness I add the solution as an answer too ... (i.e. I still think I'm young and needed the votes ...)

Starting with POI 3.13 we've added a org.apache.poi.util.LocaleUtil class, to handle Locale specific requests within POI. There's a setUserTimeZone() method to handle your issue. This needs to be set per Thread - so before opening or creating a workbook, call that function with your TimeZone object. Usually it's a good idea to clear ThreadLocals in the end of processing, especially if you deal with thread pools, so please call resetUserTimeZone() in a finally block.

Although we've taken care to prevent unnoticed/default handling of TimeZone/Locale related calls via the Forbidden Apis Tool - I wouldn't bet, that we've covered all cases. If you notice any irregularities, please open a ticket.

like image 178
kiwiwings Avatar answered Jun 02 '26 20:06

kiwiwings



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!