Java working with date formats in Excel
2011-12-23 17:34:03| Category: Java | Report | Font size Subscription
Download Lofter my photo book | In Excel, the date format, which is the number of days from January 1, 1900, such as 2009-12-24 to convert it into a digital format becomes 40171, when used in Java processing, the read will be 40171.
When a POI processes a cell of a date type in Excel, it will eventually be treated as a numeric type if it is merely to determine whether it is a date type. The correct way to do this is to first determine the type of cell is otherwise numeric type, and then determine whether the cell is a date format, if so,
Create a date format, and then display the contents of the cell in this date format. If the cell is not a date format, then it is OK to get the numeric value directly. The specific code is as follows:
if (0 = = Cell.getcelltype ()) {
Determine if a date type
if (hssfdateutil.iscelldateformatted (cell)) {
Used to convert to date format
Date d = cell.getdatecellvalue ();
DateFormat formater = new SimpleDateFormat ("Yyyy-mm-dd");
Str[k] = Formater.format (d);
}else{
Used to format numbers, preserving only the integer portion of a number
DecimalFormat df = new DecimalFormat ("########");
Str[k] = Df.format (Cell.getnumericcellvalue ());
}
Java working with date formats in Excel