一、先说一下通常的Excel 导入:
Excel 导入后,获取时间数据: <在这里 format 就是时间格式>
private static String getCellStringVal(Cell cell, String format) {
CellType cellType = cell.getCellTypeEnum();
switch (cellType) {
case NUMERIC:
String value;
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
value = TimeTool.dateToFormatTime(date, format);
} else {
double dValue = cell.getNumericCellValue();
DecimalFormat df = new DecimalFormat("0");
value = df.format(dValue);
}
return value;
case STRING:
return cell.getStringCellValue();
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
case FORMULA:
return cell.getCellFormula();
case BLANK:
return "";
case ERROR:
return String.valueOf(cell.getErrorCellValue());
default:
return "";
}
}
二、如果说我们使用的不是poi,或者其他。导入后日期为数字:
getTime();// ditNumber = 43607.4166666667
这里,我们需要保存时间戳到数据库,所以这里做了判断,且返回String。
Date、Timestamp已有,需要直接返回即可。
//Mysql支持的时间戳限制
static long minTime = Timestamp.valueOf("1970-01-01 09:00:00").getTime();
static long maxTime = Timestamp.valueOf("2038-01-19 11:00:00").getTime();
static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//判断 并转换时间格式 ditNumber = 43607.4166666667
public static String getTime(String ditNumber) {
//如果不是数字
if(!isNumeric(ditNumber)){
return null;
}
//如果是数字 小于0则 返回
BigDecimal bd = new BigDecimal(ditNumber);
int days = bd.intValue();//天数
int mills = (int) Math.round(bd.subtract(new BigDecimal(days)).doubleValue() * 24 * 3600);
//获取时间
Calendar c = Calendar.getInstance();
c.set(1900, 0, 1);
c.add(Calendar.DATE, days - 2);
int hour = mills / 3600;
int minute = (mills - hour * 3600) / 60;
int second = mills - hour * 3600 - minute * 60;
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, second);
Date d = c.getTime();//Date
Timestamp t = Timestamp.valueOf(dateFormat.format(c.getTime()));//Timestamp
try {
//时间戳区间判断
if(minTime <= d.getTime() && d.getTime() <= maxTime){
return dateFormat.format(c.getTime());
}else{
return "outOfRange";
}
} catch (Exception e) {
System.out.println("传入日期错误" + c.getTime());
}
return "Error";
}
//校验是否数据含小数点
private static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]+\\.*[0-9]*");
Matcher isNum = pattern.matcher(str);
if(!isNum.matches()){
return false;
}
return true;
}
继上,给大家提供校验日期格式的方法:
static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//传入String,返回boolean
public static boolean checkDate(String date) {
if (date != null && date.isEmpty()) {
try {
//禁止SimpleDateFormat的自动计算功能,严格解析日期
dateFormat.setLenient(false);
dateFormat.parse(date);
} catch (Exception e) {
System.out.println("传入日期错误" + date);
return false;
}
return true;
}
return false;
}
//传入java.util.Date,返回boolean
public static boolean checkDate(Date date) {
if (date != null) {
try {
//禁止SimpleDateFormat的自动计算功能,严格解析日期
dateFormat.setLenient(false);
dateFormat.format(date);
} catch (Exception e) {
System.out.println("传入日期错误" + date);
return false;
}
return true;
}
return false;
}
1715




被折叠的 条评论
为什么被折叠?



