在我看來,我們能做的唯一好事就是 - 看看時間部分是否具有零以外的價值。 我試圖用style.getDataFormatString()
和style.getDataFormat()
來檢測這個,但是他們對我的測試用例表現非常不正確。這裏是我的代碼:
private boolean dateIncludesTime() // this method only tries to detect does time part exist, but it is not sure it will succeeed
{
Date javaDate= (Date)getValue();
if (javaDate.getHours() > 0 || javaDate.getMinutes() > 0)
return true;
int formatID = apacheCell.getCellStyle().getDataFormat();
if(formatID >= 18 && formatID <=22) // https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/BuiltinFormats.html
return true;
dateFormat = apacheCell.getCellStyle().getDataFormatString();
if(dateFormat.toLowerCase().contains("h")) // format mentions hours
return true;
return false;
}
這裏就是我得到一些測試情況:
輸入2015年3月21日21:30 getDataFormatString()返回MM/DD/YY formatID = 165
輸入2016年3月21日getDataFormatString()返回MM/DD/YY formatID = 165
輸入2017年4月21日10:20 getDataFormatString()返回YYYY-MM-DD formatID = 166
輸入201 7-05-21 getDataFormatString()返回YYYY-MM-DD formatID = 166