JAVA獲取當前時間的常用方法


1、使用currentTimeMillis再格式化

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
System.out.println(df.format(System.currentTimeMillis()));

2、使用Date()

Date date=new Date(); 
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
System.out.println(df.format(date));

3、其他:

使用Calendar類,但除非單獨獲取年或者月,不建議使用這種方式,月份默認從0開始計算,需加1后才是當前月

Calendar c = Calendar.getInstance(); 
int year = c.get(Calendar.YEAR); 
int month = c.get(Calendar.MONTH)+1;
int day = c.get(Calendar.DATE);

  

還有其他的方法再羅列就有點孔乙己寫回字的意思了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM