Java 日历 get()方法

Java 日历 get()方法

java.util.Calendar.get()方法是 java.util.Calendar类的一个方法。Calendar类提供了一些方法来实现包外的具体日历系统。Calendar字段的一些例子有:年、日期、月、星期、年中之日、年中之周、分、秒、小时、上午、下午、月中之周、月中之日、日中之小时。

语法 :

public int get(int field)

其中,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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程