I'm trying to save a time in a cell in an Excel workbook using Apache POI (4.0.1).
I'm converting the time to a java.util.Date and then attempting to use the org.apache.poi.ss.usermodel.DateUtil.getExcelDate method to then save that in the cell.
What I'm finding is that as the time is in the same year as the epoch (1899), there is a hardcoded check in getExcelDate that prohibits conversion of dates with years before 1900, preventing me from doing what I was after.
Example application:
import java.util.Date;
import java.util.stream.DoubleStream;
import org.apache.poi.ss.usermodel.DateUtil;
public class POIQuery {
public static void main(final String[] args) {
final String format = "%-20s%-35s%-20s%n"; //$NON-NLS-1$
System.out.format(format, "Input Excel Date", "Java Date", "Converted Excel Date");
DoubleStream.of(0, 0.5d, 1.5d).forEach(excelDate -> {
final Date date = DateUtil.getJavaDate(excelDate);
System.out.format(format, excelDate, date, DateUtil.getExcelDate(date));
});
}
}
Output (note, as per the documentation - a "Converted Excel Date" of "-1" indicates an error).
Input Excel Date Java Date Converted Excel Date
0.0 Sun Dec 31 00:00:00 GMT 1899 -1.0
0.5 Sun Dec 31 12:00:00 GMT 1899 -1.0
1.5 Mon Jan 01 12:00:00 GMT 1900 1.5
Here you can see I'm asking for the Java Date representing the Excel date "0.5". I'm then taking the Java date provided, but POI says that there is no Excel date corresponding to that - even though I've generated it from that.
Am I missing something...?