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


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


Calendar類的get(int field_value)方法用於返回參數中給定日曆字段的值。

用法:

public int get(int field)

參數:該方法采用一個整數類型的參數field_value,表示需要返回其值的日曆。


返回值:該方法返回傳遞的字段的值。

以下示例程序旨在說明Calendar類的get()方法的用法:
示例1:

// Java Code to illustrate 
// get() Method 
  
import java.util.*; 
  
public class CalendarClassDemo 
    extends GregorianCalendar { 
    public static void main(String args[]) 
    { 
        // Creating a calendar 
        Calendar calndr = Calendar.getInstance(); 
  
        // Getting the value of all 
        // the calendar date fields 
        System.out.println("The given calendar's"
                           + " year is: " + calndr.get(Calendar.YEAR)); 
        System.out.println("The given calendar's"
                           + " month is: " + calndr.get(Calendar.MONTH)); 
        System.out.println("The given calendar's"
                           + " day is: " + calndr.get(Calendar.DATE)); 
    } 
}
輸出:
The given calendar's year is: 2019
The given calendar's month is: 1
The given calendar's day is: 13

示例2:

// Java Code to illustrate 
// get() Method 
  
import java.util.*; 
  
public class CalendarClassDemo 
    extends GregorianCalendar { 
    public static void main(String args[]) 
    { 
        // Creating a calendar object 
        Calendar calndr = new GregorianCalendar(2018, 9, 2); 
  
        // Getting the value of all 
        // the calendar date fields 
        System.out.println("The given calendar's"
                           + " year is: " + calndr.get(Calendar.YEAR)); 
        System.out.println("The given calendar's"
                           + " month is: " + calndr.get(Calendar.MONTH)); 
        System.out.println("The given calendar's"
                           + " day is: " + calndr.get(Calendar.DATE)); 
    } 
}
輸出:
The given calendar's year is: 2019
The given calendar's month is: 9
The given calendar's day is: 2

參考: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#get(int)



相關用法


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