excel導入時間文本格式轉換

有時候再在poi開源類中,若是用Excel導入時間格式是43188.468333這種格式;java

其中43188是yyyy-MM-dd,0.468333是hh:mm:ss;spa

須要將43188轉換成2018-03-29格式, 0.468333轉換成11:14:24格式.orm

以下代碼(轉換 年月日 時分秒):get

public static Date getDate(int days) {it

Calendar c = Calendar.getInstance();io

c.set(1900, 0, 1);form

c.add(Calendar.DATE, days - 2);date

return c.getTime();方法

}im

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));

              //輸出:yyyy-MM-DD 11:14:24

}

//只轉換年月日

String  deliveryDate_name = list.get ( deliveryDate_index ).trim ().toString ();//獲取到時間
Integer fomatDate = Integer.valueOf ( deliveryDate_name );//轉換時間成
 Date date_fomatDate = getDate(fomatDate);//轉換方法
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String deliveryDate_nameYMD = sdf.format ( date_fomatDate );

public static Date getDate(int days) {        Calendar c = Calendar.getInstance();        c.set(1900, 0, 1);        c.add(Calendar.DATE, days - 2);        return c.getTime();    }

相關文章
相關標籤/搜索