Java中的 Calendar internalGet() 方法及示例
Calendar 类中的internalGet(int calndr_field )方法用于将给定日历字段的值作为参数返回。
注意:该操作不涉及任何规范化或验证。
句法:
protected final int internalGet(int calndr_field)
参数:该方法采用一个参数calndr_field ,该参数引用要操作的日历字段。
返回值:该方法返回传递的参数的值。
下面的程序说明了 Calendar 类的 internalGet() 方法的工作:
示例 1:
// Java code to illustrate
// internalGet() method
import java.util.*;
public class CalendarDemo
extends GregorianCalendar {
public static void main(String args[])
{
// Creating a new calendar
CalendarDemo calndr = new CalendarDemo();
// Displaying the Current date
System.out.println("The current date is: "
+ calndr.getTime());
// Getting the Month
System.out.println("Month is: "
+ calndr
.internalGet(MONTH));
// Getting the Year
System.out.println("Year is: "
+ calndr
.internalGet(YEAR));
}
}
输出:
The current date is: Wed Feb 20 16:11:36 UTC 2019
Month is: 1
Year is: 2019
示例 2:
// Java code to illustrate
// internalGet() method
import java.util.*;
public class Calendar_Demo
extends GregorianCalendar {
public static void main(String args[])
{
// Creating a calendar object
Calendar_Demo calndr = new Calendar_Demo();
// Displaying the current date
System.out.println("Calendar: "
+ calndr
.getTime());
// Displaying the year
System.out.println("Year: "
+ calndr
.internalGet(YEAR));
// Displaying the month
System.out.println("Month: "
+ calndr
.internalGet(MONTH));
// Displaying other credentials
System.out.println("Day: "
+ calndr
.internalGet(DAY_OF_WEEK));
}
}
输出:
Calendar: Thu Feb 21 09:43:53 UTC 2019
Year: 2019
Month: 1
Day: 5
参考: https: Java/util/Calendar.html#internalGet-int-