java.util.Calendar get()方法

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

1 语法

public int get(int  field)

2 参数

Calendar的任何字段都作为参数传递。

3 返回值

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

4 示例1 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Calendar get()方法的例子
 */
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

5 示例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Calendar get()方法的例子
 */
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

6 示例3

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Calendar get()方法的例子
 */
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

 

热门文章

优秀文章