在我看来,我们能做的唯一好事就是 - 看看时间部分是否具有零以外的价值。 我试图用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