在poi开源类中,如果用Excel导入时间格式是43188.468333这种格式;
其中43188是yyyy-MM-dd,0.468333是hh:mm:ss;
需要将43188转换成2018-03-29格式, 0.468333转换成11:14:24格式.
如下代码:
public static Date getDate(int days) {
Calendar c = Calendar.getInstance();
c.set(1900, 0, 1);
c.add(Calendar.DATE, days - 2);
return c.getTime();
}
public static Date getTime(Date date, double ditNumber) {
Calendar c = Calendar.getInstance();
int mills = (int) (Math.round(ditNumber * 24 * 3600));
int hour = mills / 3600;
int minute = (mills - hour * 3600) / 60;
int second = mills - hour * 3600 - minute * 60;
c.setTime(date);
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, second);
return c.getTime();
}
public static void main(String[] args) throws java.lang.Exception {
// SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
// System.out.println(s.format(43188.468333)); Excel数字时间
int days = 43188;
double ditNumber = 0.468333;
Date date =new DateConvert().getDate(days);
Date dateTime = new DateConvert().getTime(date, ditNumber);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(dateTime));
//输出:2018-03-29 11:14:24
}
网友评论