導航:首頁 > 編程語言 > java日期轉化為數字

java日期轉化為數字

發布時間:2023-02-27 17:57:49

java導入excel時怎麼處理日期格式

在Excel中的日期格式,比如2009-12-24將其轉化為數字格式時變成了40171,在用java處理的時候,讀取的也將是40171。
如果使用POI處理Excel中的日期類型的單元格時,如果僅僅是判斷它是否為日期類型的話,最終會以NUMERIC類型來處理。正確的處理方法是先判斷單元格的類型是否則NUMERIC類型,然後再判斷單元格是否為日期格式,如果是的話,
創建一個日期格式,再將單元格的內容以這個日期格式顯示出來。如果單元格不是日期格式,那麼則直接得到NUMERIC的值就行了。具體代碼如下:

if (0 == cell.getCellType()) {

//判斷是否為日期類型

if(HSSFDateUtil.isCellDateFormatted(cell)){
//用於轉化為日期格式

Date d = cell.getDateCellValue();

DateFormat formater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

str[k] = formater.format(d);

}else{

// 用於格式化數字,只保留數字的整數部分

DecimalFormat df = new DecimalFormat("########");

str[k] = df.format(cell.getNumericCellValue());

}

㈡ Java 獲得純數字格式的時間

Java 獲得純數字格式的時間有下面三種方法

方法 一
System.currentTimeMillis();
方法 二
Calendar.getInstance().getTimeInMillis();
方法 三
new Date().getTime();
以下是效率對比:
import java.util.Calendar;
import java.util.Date;

public class TimeTest {
private static long _TEN_THOUSAND=10000;
public static void main(String[] args) {
long times=1000*_TEN_THOUSAND;
long t1=System.currentTimeMillis();
testSystem(times);
long t2=System.currentTimeMillis();
System.out.println(t2-t1);
testCalander(times);
long t3=System.currentTimeMillis();
System.out.println(t3-t2);
testDate(times);
long t4=System.currentTimeMillis();
System.out.println(t4-t3);
}

public static void testSystem(long times){//use 188
for(int i=0;i<times;i++){
long currentTime=System.currentTimeMillis();
}
}

public static void testCalander(long times){//use 6299
for(int i=0;i<times;i++){
long currentTime=Calendar.getInstance().getTimeInMillis();
}
}

public static void testDate(long times){
for(int i=0;i<times;i++){
long currentTime=new Date().getTime();
}
}
}
因為很簡單我就不加註釋了,每種方法都運行1千萬次,然後查看運行結果
187
7032
297

結果發現 System.currentTimeMillis() 這種方式速度最快
Calendar.getInstance().getTimeInMillis() 這種方式速度最慢,看看源碼會發現,Canlendar因為要處理時區問題會耗費很多的時間。
所以建議多使用第一種方式。

㈢ 如何用Java把date類型轉換成long數字

/**
*@paramargs
*/
publicstaticvoidmain(String[]args)
{
Datedate=newDate();
//返回自1970年1月1日00:00:00GMT以來此Date對象表示的毫秒數。
longtime=date.getTime();
System.out.println(time);
}

閱讀全文

與java日期轉化為數字相關的資料

熱點內容
資料庫查詢系統源碼 瀏覽:611
php5314 瀏覽:350
完美國際安裝到哪個文件夾 瀏覽:663
什麼app可以掃一掃做題 瀏覽:534
程序員編碼論壇 瀏覽:918
淘點是什麼app 瀏覽:654
中國高等植物pdf 瀏覽:448
51單片機時間 瀏覽:176
後台如何獲取伺服器ip 瀏覽:260
單片機流水燈程序c語言 瀏覽:227
程序員第二職業掙錢 瀏覽:234
運行里怎麼輸入伺服器路徑 瀏覽:833
pythonstepwise 瀏覽:502
劉一男詞彙速記指南pdf 瀏覽:56
php認證級別 瀏覽:361
方舟編譯啥時候推送 瀏覽:1003
php手機驗證碼生成 瀏覽:669
哲學思維pdf 瀏覽:9
凌達壓縮機有限公司招聘 瀏覽:527
weblogic命令部署 瀏覽:30