本文整理汇总了Java中org.apache.poi.ss.usermodel.DateUtil.getJavaDate方法的典型用法代码示例。如果您正苦于以下问题:Java DateUtil.getJavaDate方法的具体用法?Java DateUtil.getJavaDate怎么用?Java DateUtil.getJavaDate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.poi.ss.usermodel.DateUtil
的用法示例。
在下文中一共展示了DateUtil.getJavaDate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: convertToFieldType
import org.apache.poi.ss.usermodel.DateUtil; //导入方法依赖的package包/类
private Object convertToFieldType(Object cellValue, Class<?> fieldType) {
String value = String.valueOf(cellValue);
if (fieldType == Double.class || fieldType == double.class) {
return Double.valueOf(value);
}
if (fieldType == Integer.class || fieldType == int.class) {
return Double.valueOf(value).intValue();
}
if (fieldType == Short.class || fieldType == short.class) {
return Double.valueOf(value).shortValue();
}
if (fieldType == Long.class || fieldType == long.class) {
return Double.valueOf(value).longValue();
}
if (fieldType == Float.class || fieldType == float.class) {
return Double.valueOf(value).floatValue();
}
if (fieldType == Character.class || fieldType == char.class) {
return value.charAt(0);
}
if (fieldType == Date.class) {
return DateUtil.getJavaDate(Double.valueOf(value));
}
return value;
}
示例3: getNumericDateType
import org.apache.poi.ss.usermodel.DateUtil; //导入方法依赖的package包/类
@Test
public void getNumericDateType()
{
double dateDouble = 35917.0;
TimeZone utcTimeZone = TimeZone.getTimeZone(ZoneId.of("UTC"));
Date javaDate = DateUtil.getJavaDate(dateDouble, utcTimeZone);
when(cell.getCellTypeEnum()).thenReturn(NUMERIC);
when(cell.getNumericCellValue()).thenReturn(dateDouble);
when(cell.getDateCellValue()).thenReturn(javaDate);
CellStyle cellStyle = mock(CellStyle.class);
when(cell.getCellStyle()).thenReturn(cellStyle);
short dataFormat = 0x0e;
when(cellStyle.getDataFormat()).thenReturn(dataFormat);
Object val = excelEntity.get("attr1");
assertNotNull(val);
assertEquals(val, "1998-05-02t00:00");
}
示例4: getCalField
import org.apache.poi.ss.usermodel.DateUtil; //导入方法依赖的package包/类
private int getCalField(double serialDate) {
// For some reason, a date of 0 in Excel gets shown
// as the non existant 1900-01-00
if(((int)serialDate) == 0) {
switch (_dateFieldId) {
case Calendar.YEAR: return 1900;
case Calendar.MONTH: return 1;
case Calendar.DAY_OF_MONTH: return 0;
}
// They want time, that's normal
}
// TODO Figure out if we're in 1900 or 1904
Date d = DateUtil.getJavaDate(serialDate, false);
Calendar c = new GregorianCalendar();
c.setTime(d);
int result = c.get(_dateFieldId);
// Month is a special case due to C semantics
if (_dateFieldId == Calendar.MONTH) {
result++;
}
return result;
}
示例5: parseValue
import org.apache.poi.ss.usermodel.DateUtil; //导入方法依赖的package包/类
private Object parseValue( KCellType type, String vContent ) {
if ( vContent == null ) {
return null;
}
try {
switch ( type ) {
case NUMBER:
case NUMBER_FORMULA:
return Double.parseDouble( vContent );
case BOOLEAN:
case BOOLEAN_FORMULA:
return vContent.equals( "1" );
case DATE:
case DATE_FORMULA:
Double xlDate = Double.parseDouble( vContent );
return DateUtil.getJavaDate( xlDate, DATE_TZ );
case LABEL:
case STRING_FORMULA:
case EMPTY:
default:
return vContent;
}
} catch ( Exception e ) {
return vContent;
}
}
示例6: 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));
}
}
示例7: getValue
import org.apache.poi.ss.usermodel.DateUtil; //导入方法依赖的package包/类
/** 从表格之中读取数据并进行处理
* @param fieldInfo
* @param cell 当前cell
* @return
* @throws Exception
*/
public static Object getValue(ImportFieldInfo fieldInfo, Cell cell) throws Exception{
int size = fieldInfo.getTypeChain().size();
Class<?> type = fieldInfo.getTypeChain().get(size - 1);
String dateFormat = fieldInfo.getDateFormat();
int cellType = cell.getCellType();
Object obj = null ;
switch (cellType) {
case Cell.CELL_TYPE_BLANK:
return null;
case Cell.CELL_TYPE_BOOLEAN:
obj = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_STRING:
obj = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
if(DateUtil.isCellDateFormatted(cell)){
obj = DateUtil.getJavaDate(cell.getNumericCellValue());
}else if(Number.class.isAssignableFrom(type) || ClassUtils.isBaseNumberType(type)){
//当pojo字段类型是数字型时以数字形式获取
obj = cell.getNumericCellValue();
}else{
//其他类型都以string获取
obj = DATA_FORMATTER.formatCellValue(cell);
}
break;
case Cell.CELL_TYPE_ERROR:
return null;
}
if(fieldInfo.getImportProcessor() != null){
obj = fieldInfo.getImportProcessor().process(obj);
}
obj = ConvertUtils.convertIfNeccesary(obj, type, dateFormat);
return obj;
}
示例8: visitCellValueNumeric
import org.apache.poi.ss.usermodel.DateUtil; //导入方法依赖的package包/类
@Override
public void visitCellValueNumeric(Column column, Object source, double value) {
TimestampParser parser = getTimestampParser(column);
TimeZone tz = parser.getDefaultTimeZone().toTimeZone();
Date date = DateUtil.getJavaDate(value, tz);
Timestamp t = Timestamp.ofEpochMilli(date.getTime());
pageBuilder.setTimestamp(column, t);
}
示例9: readCell
import org.apache.poi.ss.usermodel.DateUtil; //导入方法依赖的package包/类
private static final Object readCell(final Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
return DateUtil.getJavaDate(cell.getNumericCellValue());
}
return cell.getNumericCellValue();
case Cell.CELL_TYPE_BOOLEAN:
return cell.getBooleanCellValue();
default:
return cell.getStringCellValue();
}
}
示例10: parseDateTime
import org.apache.poi.ss.usermodel.DateUtil; //导入方法依赖的package包/类
public static DateTime parseDateTime(String str) {
try {
return DATE_FORMATTER.parseDateTime(str);
} catch (IllegalArgumentException e) {
try {
double n = Double.parseDouble(str);
return new DateTime(DateUtil.getJavaDate(n));
} catch (NumberFormatException e2) {
//Ignore
}
throw e;//Rethrow IllegalArgumentException
}
}
示例11: getValue
import org.apache.poi.ss.usermodel.DateUtil; //导入方法依赖的package包/类
public static Object getValue(Cell cell) {
Object result = null;
//タイプ分岐
switch (cell.getCellType())
{
case Cell.CELL_TYPE_BLANK:
result = "";
break;
case Cell.CELL_TYPE_BOOLEAN:
result = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_ERROR:
result = cell.getErrorCellValue();
break;
case Cell.CELL_TYPE_FORMULA:
result = cell.getCellFormula();
break;
case Cell.CELL_TYPE_NUMERIC:
double num = cell.getNumericCellValue();
if (DateUtil.isCellDateFormatted(cell)) {
///日付型
result = DateUtil.getJavaDate(num);
} else {
// 数値型
result = cell.getNumericCellValue();
}
break;
case Cell.CELL_TYPE_STRING:
result = cell.getStringCellValue();
break;
}
return result;
}
示例12: getCellDate
import org.apache.poi.ss.usermodel.DateUtil; //导入方法依赖的package包/类
private static Date getCellDate(Cell cell) {
if (HSSFDateUtil.isCellDateFormatted(cell)) {// 处理日期格式、时间格式
return cell.getDateCellValue();
}
return DateUtil.getJavaDate(cell.getNumericCellValue());
}
示例13: addEvent
import org.apache.poi.ss.usermodel.DateUtil; //导入方法依赖的package包/类
private void addEvent(final Map<String, Serializable> input) {
if (!this.uniqueEvents.contains(input)) {
final Map<String, Serializable> values = new HashMap<String, Serializable>();
for (final TypeTreeNode attribute : this.eventType.getRootLevelValueTypes()) {
try {
final String attributeExpression = attribute.getAttributeExpression();
Serializable attributeValue = null;
if (input.get(attributeExpression) != null) {
if (attribute.getType() == AttributeTypeEnum.DATE) {
if (input.get(attributeExpression) instanceof String) {
attributeValue = DateUtils.parseDate((String) input.get(attributeExpression));
} else if (input.get(attributeExpression) instanceof Double) {
attributeValue = DateUtil.getJavaDate((Double) input.get(attributeExpression));
} else if (input.get(attributeExpression) instanceof Long) {
attributeValue = DateUtil.getJavaDate((Long) input.get(attributeExpression));
} else {
attributeValue = input.get(attributeExpression);
}
} else if (attribute.getType() == AttributeTypeEnum.INTEGER) {
if (input.get(attributeExpression) instanceof Long) {
attributeValue = ((Long) input.get(attributeExpression)).longValue();
} else if (input.get(attributeExpression) instanceof Double) {
attributeValue = ((Double) input.get(attributeExpression)).longValue();
} else if (input.get(attributeExpression) instanceof Integer) {
attributeValue = ((Integer) input.get(attributeExpression)).longValue();
} else {
attributeValue = (new Long(((String) input.get(attributeExpression)))).longValue();
}
} else if (attribute.getType() == AttributeTypeEnum.FLOAT) {
if (input.get(attributeExpression) instanceof Long) {
attributeValue = ((Long) input.get(attributeExpression)).doubleValue();
} else if (input.get(attributeExpression) instanceof Double) {
attributeValue = ((Double) input.get(attributeExpression)).doubleValue();
} else if (input.get(attributeExpression) instanceof Integer) {
attributeValue = ((Integer) input.get(attributeExpression)).doubleValue();
} else {
attributeValue = (new Double(((String) input.get(attributeExpression)))).doubleValue();
}
} else {
attributeValue = input.get(attributeExpression).toString();
}
}
values.put(attributeExpression, attributeValue);
} catch (final Exception e) {
e.printStackTrace();
}
}
// System.out.println(values.toString());
final EapEvent event = new EapEvent(this.eventType, new Date(), values);
final Object timestamp = input.get(event.getEventType().getTimestampName());
if (timestamp instanceof Date) {
event.setTimestamp((Date) timestamp);
}
Broker.getEventImporter().importEvent(event);
// System.out.println("Event created: " + event);
this.numberOfEventsFired++;
this.uniqueEvents.add(input);
}
}
示例14: getDateCellValue
import org.apache.poi.ss.usermodel.DateUtil; //导入方法依赖的package包/类
public Date getDateCellValue() {
return DateUtil.getJavaDate(getNumericCellValue());
}
示例15: createValue
import org.apache.poi.ss.usermodel.DateUtil; //导入方法依赖的package包/类
private String createValue() {
if (_value.length() == 0) {
return null;
}
switch (_dataType) {
case BOOL:
char first = _value.charAt(0);
return first == '0' ? "false" : "true";
case ERROR:
logger.warn("Error-cell occurred: {}", _value);
return _value.toString();
case FORMULA:
return _value.toString();
case INLINESTR:
XSSFRichTextString rtsi = new XSSFRichTextString(_value.toString());
return rtsi.toString();
case SSTINDEX:
String sstIndex = _value.toString();
int idx = Integer.parseInt(sstIndex);
XSSFRichTextString rtss = new XSSFRichTextString(_sharedStringTable.getEntryAt(idx));
return rtss.toString();
case NUMBER:
final String numberString = _value.toString();
if (_formatString != null) {
DataFormatter formatter = getDataFormatter();
if (HSSFDateUtil.isADateFormat(_formatIndex, _formatString)) {
Date date = DateUtil.getJavaDate(Double.parseDouble(numberString));
return DateUtils.createDateFormat().format(date);
}
return formatter.formatRawCellContents(Double.parseDouble(numberString), _formatIndex, _formatString);
} else {
if (numberString.endsWith(".0")) {
// xlsx only stores doubles, so integers get ".0" appended
// to them
return numberString.substring(0, numberString.length() - 2);
}
return numberString;
}
default:
logger.error("Unsupported data type: {}", _dataType);
return "";
}
}