1、Java.util.Date
Date date = new Date();
通過Date類來獲取當(dāng)前時(shí)間,比較常用。需要使用Java.util.Date類,速度一般。
2、System.currentTimeMillis()
通過System類中的currentTimeMillis方法來獲取當(dāng)前時(shí)間,無需導(dǎo)入類,速度最快。
此方法優(yōu)勢是不受時(shí)區(qū)的影響,但是得到結(jié)果是時(shí)間戳的格式
System.currentTimeMillis();
//結(jié)果為 153452345528345 格式
可以通過代碼將時(shí)間戳轉(zhuǎn)化為我們可以理解的格式:
SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
Date date = new Date(System.currentTimeMillis());
System.out.println(formatter.format(date));
轉(zhuǎn)換后格式如下:
2023-05-13 at 11:34:12 CET
3、Calendar類
Calendar 類專門用來轉(zhuǎn)換特定時(shí)刻和日歷字段之間的日期和時(shí)間
Calendar calendar = Calendar.getInstance();
同時(shí)它還支持更自由的取時(shí)間文章來源:http://www.zghlxwxcb.cn/news/detail-565461.html
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int date = calendar.get(Calendar.DATE);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
非常靈活文章來源地址http://www.zghlxwxcb.cn/news/detail-565461.html
到了這里,關(guān)于Java獲取當(dāng)前時(shí)間的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!