Java 日历 internalGet()方法及实例

Java 日历 internalGet()方法及实例

Calendar类中的 internalGet(int calndr_field ) 方法用于返回作为参数的给定日历字段的值。

注意:该操作不涉及任何标准化或验证。

语法

protected final int internalGet(int calndr_field)

参数: 该方法需要一个参数calndr_field,指的是要操作的日历字段。

返回值: 该方法返回所传递参数的值。

下面的程序说明了日历类的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://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#internalGet-int-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Java 日历