System.currentTimeMillis()用法及其计算方式与时间的单位转换
生活随笔
收集整理的這篇文章主要介紹了
System.currentTimeMillis()用法及其计算方式与时间的单位转换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
System.currentTimeMillis()的作用是返回當前的計算機時間,格式為當前計算機時間和GMT時間(格林威治時間)1970年1月1號0時0分0秒所差的毫秒數
時間的單位轉換
1秒=1000毫秒(ms)
1分鐘=60秒
1小時=60分鐘=3600秒
用途一:計算某任務 耗費的毫秒
//開始時間long start = System.currentTimeMillis();for (int i = 0; i < 5; i++) {Thread.sleep(10);}//結束時間long end = System.currentTimeMillis();System.out.println("for循環耗時" + (end - start) + "毫秒");用途二:獲得當前的系統時間
//可以直接把這個方法強制轉換成date類型Date nowTime = new Date(System.currentTimeMillis());//設定顯示格式SimpleDateFormat sdFormatter = new SimpleDateFormat("yyyy-MM-dd");//按指定格式轉換String now = sdFormatter.format(nowTime);System.out.println(now);用途三:用當前毫秒數給文件命名等
File f = new File("c:\\"+System.currentTimeMillis() + "");f.createNewFile();總結
以上是生活随笔為你收集整理的System.currentTimeMillis()用法及其计算方式与时间的单位转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android获取存储和打印输出Logc
- 下一篇: Android 中三种启用线程的方法