當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Calendar.get()用法及代碼示例


java.util.Calendar.get()方法是java.util.Calendar類的方法。 Calendar類提供了一些用於在包外部實現具體日曆係統的方法。日曆字段的一些示例是:YEAR,DATE,MONTH,DAY_OF_WEEK,DAY_OF_YEAR,WEEK_OF_YEAR,MINUTE,SECOND,HOUR,AM_PM,WEEK_OF_MONTH,DAY_OF_WEEK_IN_MONTH,HOUR_OF_DAY。

用法:

public int get(int field)

where, field represents the given calendar
field and the function returns the value of
given field.

異常:如果指定的字段超出範圍,則拋出ArrayIndexOutOfBoundsException。


應用範圍:
示例1:獲取日期,月份,年份

// Java code to implement calendar.get() function 
import java.util.*; 
  
class GFG { 
      
// Driver code 
public static void main(String[] args) { 
  
    // creating a calendar 
    Calendar c = Calendar.getInstance(); 
  
    // get the value of DATE field 
    System.out.println("Day : " + 
                        c.get(Calendar.DATE)); 
      
    // get the value of MONTH field 
    System.out.println("Month : " + 
                        c.get(Calendar.MONTH)); 
                      
    // get the value of YEAR field 
    System.out.println("Year : " +  
                        c.get(Calendar.YEAR)); 
} 
}

輸出:

Day : 1
Month : 2
Year : 2018

示例2:要獲取星期幾,每年的某天,每月的某周,每年的某周。

// Java Code of calendar.get() function 
import java.util.*; 
  
class GFG { 
      
// Driver code 
public static void main(String[] args) { 
  
    // creating a calendar 
    Calendar c = Calendar.getInstance(); 
  
    // get the value of DATE_OF_WEEK field 
    System.out.println("Day of week : " +  
                        c.get(Calendar.DAY_OF_WEEK)); 
                          
    // get the value of DAY_OF_YEAR field 
    System.out.println("Day of year : " + 
                        c.get(Calendar.DAY_OF_YEAR)); 
                          
    // get the value of WEEK_OF_MONTH field 
    System.out.println("Week in Month : " +  
                        c.get(Calendar.WEEK_OF_MONTH)); 
                          
    // get the value of WEEK_OF_YEAR field 
    System.out.println("Week in Year : " +  
                        c.get(Calendar.WEEK_OF_YEAR)); 
  
                      
    // get the value of DAY_OF_WEEK_IN_MONTH field 
    System.out.println("Day of Week in Month : " + 
                        c.get(Calendar.DAY_OF_WEEK_IN_MONTH)); 
} 
}

輸出:

Day of week : 5
Day of year : 60
Week in Month : 1
Week in Year : 9
Day of Week in Month : 1

示例3:要獲取小時,分鍾,秒和AM_PM。

// Implementation of calendar.get() 
// function in Java 
import java.util.*; 
  
class GFG { 
  
    // Driver code 
    public static void main(String[] args) 
    { 
        // creating a calendar 
        Calendar c = Calendar.getInstance(); 
  
        // get the value of HOUR field 
        System.out.println("Hour : " + c.get(Calendar.HOUR)); 
  
        // get the value of MINUTE field 
        System.out.println("Minute : " + c.get(Calendar.MINUTE)); 
  
        // get the value of SECOND field 
        System.out.println("Second : " + c.get(Calendar.SECOND)); 
  
        // get the value of AM_PM field 
        System.out.println("AM or PM : " + c.get(Calendar.AM_PM)); 
  
        // get the value of HOUR_OF_DAY field 
        System.out.println("Hour (24-hour clock) : " + c.get(Calendar.HOUR_OF_DAY)); 
    } 
}

輸出:

Hour : 6
Minute : 51
Second : 53
AM or PM : 0
Hour (24-hour clock) : 6


相關用法


注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Calendar.get() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。