java 時間戳記的擷取方式

來源:互聯網
上載者:User

標籤:time()   end   技術   cal   span   gif   new   imei   mat   

擷取目前時間戳
//方法 一System.currentTimeMillis();//方法 二Calendar.getInstance().getTimeInMillis();//方法 三new Date().getTime();
擷取目前時間
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設定日期格式String date = df.format(new Date());// new Date()為擷取當前系統時間,也可使用目前時間戳
擷取時間戳記三種方法執行效率比較:
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();        }    } }
執行結果:
133
2372
137

Calendar.getInstance().getTimeInMillis() 這種方式速度最慢,這是因為Canlendar要處理時區問題會耗費較多的時間。

java 時間戳記的擷取方式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.