本文整理匯總了Java中org.apache.poi.ss.usermodel.DateUtil.isValidExcelDate方法的典型用法代碼示例。如果您正苦於以下問題:Java DateUtil.isValidExcelDate方法的具體用法?Java DateUtil.isValidExcelDate怎麽用?Java DateUtil.isValidExcelDate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.poi.ss.usermodel.DateUtil
的用法示例。
在下文中一共展示了DateUtil.isValidExcelDate方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: formatRawCellContents
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
/**
* 判斷是否是日期,若是返回日期字符串,否則返回數字字符串
*
* @param value
* @param formatIndex
* @param formatString
* @return
*/
private String formatRawCellContents(double value, int formatIndex, String formatString) {
// Is it a date?
if (DateUtil.isADateFormat(formatIndex, formatString)) {
if (DateUtil.isValidExcelDate(value)) {
Date d = DateUtil.getJavaDate(value);
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
return f.format(d);
}
}
String _value = NumberToTextConverter.toText(value);
if (_value.indexOf('E') > -1) {
//若是科學計數法,則轉換為非科學計數法
return new BigDecimal(value).toString();
}
return _value;
}
示例2: isCellDateFormatted
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
/**
* POIのDateUtilにあるメソッドが日本語を含む日付書式を正しく扱ってくれないので自力実裝
*/
public static boolean isCellDateFormatted(Cell cell, double d) {
if (cell == null) {
return false;
}
boolean bDate = false;
if (DateUtil.isValidExcelDate(d)) {
CellStyle style = cell.getCellStyle();
if (style == null) {
return false;
}
int i = style.getDataFormat();
String f = style.getDataFormatString();
bDate = isADateFormat(i, f);
}
return bDate;
}
示例3: getDate
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
@Override
public Date getDate(int columnIndex) throws ParseException {
String dateValue = getValue(columnIndex);
if (dateValue == null) {
return null;
}
switch (getCellType(columnIndex)) {
case NUMBER:
case DATE:
// XLSX stores dates as double values
double dateAsDouble = Double.parseDouble(dateValue);
// Use POI methods to convert value to Date java object
if (DateUtil.isValidExcelDate(dateAsDouble)) {
return DateUtil.getJavaDate(dateAsDouble, xlsxWorkbook.isDate1904);
} else {
throw new ParseException(new ParsingError(getCurrentRow() + 1, columnIndex,
ParsingError.ErrorCode.UNPARSEABLE_DATE, dateValue));
}
case INLINE_STRING:
case SHARED_STRING:
case STRING:
// In case a date is stored as String, we try to parse it here
String dateString = dateValue;
try {
return dateFormatProvider.geDateFormat().parse(dateString);
} catch (java.text.ParseException e) {
throw new ParseException(new ParsingError(getCurrentRow() + 1, columnIndex,
ParsingError.ErrorCode.UNPARSEABLE_DATE, dateString));
}
default:
throw new ParseException(new ParsingError(getCurrentRow() + 1, columnIndex,
ParsingError.ErrorCode.UNPARSEABLE_DATE, dateValue));
}
}
示例4: formatRawCellContents
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
@Override
public String formatRawCellContents(double value, int formatIndex, String formatString, boolean use1904Windowing) {
// TDP-1656 (olamy) for some reasons poi use date format with only 2 digits for years
// even the excel data ws using 4 so force the pattern here
if ( DateUtil.isValidExcelDate( value) && StringUtils.countMatches( formatString, "y") == 2) {
formatString = StringUtils.replace(formatString, "yy", "yyyy");
}
if (DateUtil.isValidExcelDate(value) && StringUtils.countMatches(formatString, "Y") == 2) {
formatString = StringUtils.replace(formatString, "YY", "YYYY");
}
return super.formatRawCellContents(value, formatIndex, formatString, use1904Windowing);
}
示例5: isCellDateFormatted
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
/**
* DateUtilがLocalizeされたフォーマット(年,月,日等を含むフォーマット)に対応していないため、
* フォーマットの""で囲まれた文字列を除去するようにして対応。
* DateUtilが対応されたらそっちを使用する。
* Bug 47071として報告済み
*
* @param cell 対象セル
*/
public static boolean isCellDateFormatted( Cell cell) {
if ( cell == null) {
return false;
}
boolean bDate = false;
double d = cell.getNumericCellValue();
if ( DateUtil.isValidExcelDate( d)) {
CellStyle style = cell.getCellStyle();
if ( style == null) {
return false;
}
int i = style.getDataFormat();
String fs = style.getDataFormatString();
if ( fs != null) {
// And '"any"' into ''
while ( fs.contains( "\"")) {
int beginIdx = fs.indexOf( "\"");
if ( beginIdx == -1) {
break;
}
int endIdx = fs.indexOf( "\"", beginIdx + 1);
if ( endIdx == -1) {
break;
}
fs = fs.replaceFirst( Pattern.quote( fs.substring( beginIdx, endIdx + 1)), "");
}
}
bDate = DateUtil.isADateFormat( i, fs);
}
return bDate;
}
示例6: isCellDateFormatted
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
private boolean isCellDateFormatted( Cell cell ) {
if (cell == null) return false;
if ( ! DateUtil.isValidExcelDate(cell.getNumericCellValue()) ) return false;
CellStyle style = cell.getCellStyle();
if( style == null ) return false;
int formatIndex = style.getDataFormat();
String format = style.getDataFormatString();
// Apache poi's missing logic
format = format.replaceAll( "([^\\\\])\".*?[^\\\\]\"", "$1" );
return DateUtil.isADateFormat( formatIndex, format) ;
}
示例7: doFormatCellValue
import org.apache.poi.ss.usermodel.DateUtil; //導入方法依賴的package包/類
private FormattedValue doFormatCellValue(Cell cell, FormulaEvaluator evaluator) {
if (cell == null) {
return FormattedValue.EMPTY;
}
int cellType = cell.getCellType();
if (cellType == Cell.CELL_TYPE_FORMULA) {
if (evaluator == null) {
cellType = cell.getCachedFormulaResultType();
if (cellType == Cell.CELL_TYPE_FORMULA) {
return new FormattedValue(cell.getCellFormula(), FormattedValue.Type.FORMULA, cell.getCellFormula());
}
} else {
cellType = evaluator.evaluateFormulaCell(cell);
}
}
String ret = "";
Object rawdata = null;
FormattedValue.Type type = null;
Color color = null;
if (cellType == Cell.CELL_TYPE_NUMERIC) {
if (cell.getCellStyle() == null) {
ret = this.numberFormatter.formatCellValue(cell, evaluator);
type = FormattedValue.Type.NUMBER;
} else {
boolean bDate = false;
short idx = cell.getCellStyle().getDataFormat();
String fmt = getLocalizedDateFormat(idx);
//InternalDateFormatはPOIにまかせる
bDate = DateUtil.isValidExcelDate(cell.getNumericCellValue()) &&
(fmt != null || (idx > 0x31 && ExcelUtils.isADateFormat(idx, cell.getCellStyle().getDataFormatString())));
if (bDate) {
ret = formatDate(cell, fmt);
rawdata = cell.getDateCellValue();
type = FormattedValue.Type.DATE;
} else {
ret = this.numberFormatter.formatCellValue(cell, evaluator);
rawdata = cell.getNumericCellValue();
type = FormattedValue.Type.NUMBER;
}
FormatHolder holder = getFormatHolder(cell, fmt);
if (holder != null) {
color = holder.getFormatInfo(cell.getNumericCellValue()).getColor();
}
}
} else {
switch (cellType) {
case Cell.CELL_TYPE_BLANK:
return FormattedValue.EMPTY;
case Cell.CELL_TYPE_ERROR:
ret = String.valueOf(cell.getErrorCellValue());
type = FormattedValue.Type.ERROR;
break;
case Cell.CELL_TYPE_STRING:
ret = cell.getRichStringCellValue().getString();
type = FormattedValue.Type.STRING;
break;
case Cell.CELL_TYPE_BOOLEAN:
ret = String.valueOf(cell.getBooleanCellValue());
type = FormattedValue.Type.BOOLEAN;
break;
default:
throw new IllegalStateException();
}
}
return new FormattedValue(ret, type, rawdata != null ? rawdata : ret, color);
}