POI Java Date to Excel seconds difference

2019-08-06 06:15发布

问题:

guys.

I'm having some trouble when exporting dates from Java to Excel cells using Apache POI library.

It's a marvelous tool and very easy to understand and use, but right now it is giving me some headaches!

Here's the thing: I'm writing an application that validates certifications using a code made out of 2 letters followed by 9 digits. The last 4 digits are made by the sum of each number representing day, month and year as well as hour, minute and seconds in the instant the code generation took place. e.g. 03/31/2015 17:30:32 (24 hour format) => 3 + 31 + 2015 + 17 + 30 + 32 = 2128. Everything works fine, code gets generated, workbook gets a new line with each data, including date of generation. But when I open workbook to see the result, generation date cell gets slightly different from actual date value and doing math again results in those 4 last digits giving a different sum, something like 2126 or 2130 (varies each time code runs).

I google'd and found its gotta be related to how MS Excel and Java handles Date calculation. Excel use days since 01/01/1900 and Java miliseconds since 01/01/1970.

Does someone know any kind of workaround to it?

Thanks.