Java 1.8獲取日期和時間


 public class DateTimeTest {
   public static void main(String[] args) {
     Calendar cal = Calendar.getInstance();
     System.out.println(cal.get(Calendar.YEAR));
     System.out.println(cal.get(Calendar.MONTH)); // 0 - 11
     System.out.println(cal.get(Calendar.DATE));
     System.out.println(cal.get(Calendar.HOUR_OF_DAY));
     System.out.println(cal.get(Calendar.MINUTE));
     System.out.println(cal.get(Calendar.SECOND));
     // Java 8
     LocalDateTime dt = LocalDateTime.now();
     System.out.println(dt.getYear()); 
     System.out.println(dt.getMonthValue()); // 1 - 12
     System.out.println(dt.getDayOfMonth());
     System.out.println(dt.getHour());
     System.out.println(dt.getMinute());
     System.out.println(dt.getSecond());
 }

 


免責聲明!

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



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