基于Calendar獲取當(dāng)前時間的性能比較
除了獲取時間用Date和SimperFormat,還可用Calendar類方法獲取時間。
一、Calendar 類常用的獲取時間以及時區(qū)的方法:
Calendar calendar = Calendar.getInstance(); System.out.println("目前時間: " + calendar.getTime()); System.out.println("Calendar時區(qū): " + calendar.getTimeZone().getID()); System.out.println("user.timezone: " + System.getProperty("user.timezone")); System.out.println("user.country: " + System.getProperty("user.country")); System.out.println("默認(rèn)時區(qū): " + TimeZone.getDefault().getID());
運(yùn)行結(jié)果:
目前時間: Tue May 28 23:09:31 CST 2019
Calendar時區(qū): Asia/Shanghai
user.timezone: Asia/Shanghai
user.country: CN
默認(rèn)時區(qū): Asia/Shanghai
二、獲取當(dāng)前時間性能比較
long start1 = System.currentTimeMillis(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設(shè)置日期格式 System.out.println(sdf.format(new Date()));// new Date()為獲取當(dāng)前系統(tǒng)時間 long end1 = System.currentTimeMillis(); System.out.println((end1 - start1) + "ms");
運(yùn)行結(jié)果:
2019-05-28 23:14:14
55ms
long start2 = System.currentTimeMillis(); Calendar calendar = Calendar.getInstance();//可以對每個時間域單獨(dú)修改 System.out.println(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DATE) + " " + calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND)); long end2 = System.currentTimeMillis(); System.out.println((end2 - start2) + "ms");
運(yùn)行結(jié)果:
2019-4-28 23:15:22
36ms
顯然,第二種的Calendar獲取當(dāng)前時間的性能比SimpleDateFormat的要快
三、獲取零點(diǎn)時間的性能比較
long start3 = System.currentTimeMillis(); Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date zero = calendar.getTime(); long end3 = System.currentTimeMillis(); System.out.println(zero); System.out.println((end3 - start3) + "ms");
運(yùn)行結(jié)果:
Tue May 28 00:00:00 CST 2019
34ms
long start4 = System.currentTimeMillis(); long current = System.currentTimeMillis(); long zero = current/(1000*3600*24)*(1000*3600*24) - TimeZone.getDefault().getRawOffset(); System.out.println(zero); long end4 = System.currentTimeMillis(); System.out.println((end4 - start4) + "ms");
運(yùn)行結(jié)果:
1558972800000
11ms
這里current表示毫秒,那么除以了1000 * 3600 * 24得到天數(shù),但是可能不是整數(shù)天,但是因?yàn)閠是long型,那么小數(shù)部分沒有了,再去乘以1000 * 3600 * 24,就變成整數(shù)天數(shù)所對應(yīng)的毫秒了。
TimeZone.getDefault().getRawOffset() 計算夏令時和返回當(dāng)前時區(qū)與格林尼治時間的偏差。
顯然,這里獲取零點(diǎn)時間強(qiáng)烈推薦第二種方法,性能比第一種高三倍左右。
Calendar獲取當(dāng)前年份、月份、日期
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class TestDate { /** * 獲取當(dāng)前年份、月份、日期 * @param args */ public static void main(String[] args) { Calendar cale = null; cale = Calendar.getInstance(); int year = cale.get(Calendar.YEAR); int month = cale.get(Calendar.MONTH) + 1; int day = cale.get(Calendar.DATE); int hour = cale.get(Calendar.HOUR_OF_DAY); int minute = cale.get(Calendar.MINUTE); int second = cale.get(Calendar.SECOND); int dow = cale.get(Calendar.DAY_OF_WEEK); int dom = cale.get(Calendar.DAY_OF_MONTH); int doy = cale.get(Calendar.DAY_OF_YEAR); System.out.println("Current Date: " + cale.getTime()); System.out.println("Year: " + year); System.out.println("Month: " + month); System.out.println("Day: " + day); System.out.println("Hour: " + hour); System.out.println("Minute: " + minute); System.out.println("Second: " + second); System.out.println("Day of Week: " + dow); System.out.println("Day of Month: " + dom); System.out.println("Day of Year: " + doy); // 獲取當(dāng)月第一天和最后一天 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); String firstday, lastday; // 獲取前月的第一天 cale = Calendar.getInstance(); cale.add(Calendar.MONTH, 0); cale.set(Calendar.DAY_OF_MONTH, 1); firstday = format.format(cale.getTime()); // 獲取前月的最后一天 cale = Calendar.getInstance(); cale.add(Calendar.MONTH, 1); cale.set(Calendar.DAY_OF_MONTH, 0); lastday = format.format(cale.getTime()); System.out.println("本月第一天和最后一天分別是 : " + firstday + " and " + lastday); // 獲取當(dāng)前日期字符串 Date d = new Date(); System.out.println("當(dāng)前日期字符串1:" + format.format(d)); System.out.println("當(dāng)前日期字符串2:" + year + "/" + month + "/" + day + " " + hour + ":" + minute + ":" + second); } }
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot定時任務(wù)備份mysql數(shù)據(jù)庫的實(shí)現(xiàn)示例
為了防止數(shù)據(jù)庫被清庫或者誤刪數(shù)據(jù)庫的情況,所以需要定時將mysql數(shù)據(jù)庫中的數(shù)據(jù)進(jìn)行備份,本文主要介紹了springboot定時任務(wù)備份mysql數(shù)據(jù)庫的實(shí)現(xiàn)示例,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-03-03基于Java實(shí)現(xiàn)無向環(huán)和有向環(huán)的檢測
這篇文章主要介紹了如何在?Java?中實(shí)現(xiàn)無向環(huán)和有向環(huán)的檢測,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下2022-04-04詳解Spring Boot Oauth2緩存UserDetails到Ehcache
這篇文章主要介紹了詳解Spring Boot Oauth2緩存UserDetails到Ehcache,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08SpringMVC獲取請求參數(shù)和域?qū)ο蠊蚕頂?shù)據(jù)的示例代碼
這篇文章主要給大家介紹了SpringMVC獲取請求參數(shù)和域?qū)ο蠊蚕頂?shù)據(jù)的示例代碼,文中通過代碼示例給大家介紹的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下2023-12-12