Java教程

Java Calendar get()方法

Calender类的get()方法返回传递给它的参数字段的值。像DAY_OF_MONTH,MONTH,YEAR,WEEK,WEEK_OF_YEAR等字段作为参数传递给此方法。

语法

public int get(int field)

参数

field: 日历的任何字段都作为参数传递。

返回

传递的返回值字段作为整数形式的参数。

抛出

ArrayIndexOutOfBoundsException

示例1

import java.util.Calendar;
public class CalendargetExample1 {
    public static void main(String[] args) {
        // creating a calendar named as cal1
    Calendar cal1 = Calendar.getInstance();
        // DATE field is passed as parameter to get method
    System.out.println("Day of month is: " +
                        cal1.get(Calendar.DATE));
        // MONTH field DATE field is passed as parameter to get method
    System.out.println("Month of year is: " +
                        cal1.get(Calendar.MONTH));
        // YEAR field DATE field is passed as parameter to get method
    System.out.println("and year is : " +
                        cal1.get(Calendar.YEAR));
        System.out.println("Today is "+cal1.get(Calendar.DATE) + " Day of " + cal1.get(Calendar.MONTH)
    + " Month of the year " + cal1.get(Calendar.YEAR) );
    }
}
输出:
Day of month is: 2
Month of year is: 7
and year is : 2018
Today is 2 Day of 7 Month of the year 2018

示例2

import java.util.Calendar;
public class CalendargetExample2 {
    public static void main(String[] args) {
        // creating a calendar named as cal1
    Calendar cal1 = Calendar.getInstance();
        // HOUR field is passed as parameter to get method
    System.out.println("Hour is : " +
                        cal1.get(Calendar.HOUR));
        // MINUTE field DATE field is passed as parameter to get method
    System.out.println("Minute is: " +
                        cal1.get(Calendar.MINUTE));
        // SECOND field DATE field is passed as parameter to get method
    System.out.println("Second is" +
                        cal1.get(Calendar.SECOND));
        System.out.println("Time : "+cal1.get(Calendar.HOUR) + ":" + cal1.get(Calendar.MINUTE)
    + ":" + cal1.get(Calendar.SECOND) );
    }
}
输出:
Hour is : 8
Minute is: 1
Second is1
Time : 8:1:1

示例3

import java.util.Calendar;
public class CalendargetExample3 {
    public static void main(String[] args) {
        // creating a calendar
    Calendar cal1 = Calendar.getInstance();
        // Print the value of Calendar
    System.out.println("Date when objectt is created : " + cal1.getTime());
        cal1.add((Calendar.MONTH), 10);
        cal1.add((Calendar.YEAR), 2017);
        cal1.add((Calendar.DAY_OF_MONTH), 23);
        System.out.println("date after using add method : " + cal1.getTime());
        System.out.println("HOUR : " +
                        cal1.get(Calendar.HOUR));
        // print the value of MONTH field
    System.out.println("MINUTE : " +
                        cal1.get(Calendar.MINUTE));
        // print the value of YEAR field
    System.out.println("SECOND : " +
                        cal1.get(Calendar.SECOND));
    }
}
输出:
Date when objectt is created : Thu Aug 02 08:07:16 PDT 2018
date after using add method : Wed Jun 25 08:07:16 PDT 4036
HOUR : 8
MINUTE : 7
SECOND : 16
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4