java.util.Calendar.get()方法是java.util.Calendar类的方法。 Calendar类提供了一些用于在包外部实现具体日历系统的方法。日历字段的一些示例是:YEAR,DATE,MONTH,DAY_OF_WEEK,DAY_OF_YEAR,WEEK_OF_YEAR,MINUTE,SECOND,HOUR,AM_PM,WEEK_OF_MONTH,DAY_OF_WEEK_IN_MONTH,HOUR_OF_DAY。
用法:
public int get(int field) where, field represents the given calendar field and the function returns the value of given 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
相关用法
- Java Java lang.Long.numberOfLeadingZeros()用法及代码示例
- Java Java.util.Collections.rotate()用法及代码示例
- Java Java lang.Long.highestOneBit()用法及代码示例
- Java Java lang.Long.byteValue()用法及代码示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代码示例
- Java Java lang.Long.lowestOneBit()用法及代码示例
- Java Java lang.Long.reverse()用法及代码示例
- Java Java.util.Collections.disjoint()用法及代码示例
- Java Java.math.BigInteger.modInverse()用法及代码示例
- Java Clock tickMinutes()用法及代码示例
- Java Java.math.BigInteger.probablePrime()用法及代码示例
- Java Java.util.ArrayList.addall()用法及代码示例
- Java Java.lang.string.replace()用法及代码示例
注:本文由纯净天空筛选整理自Sahil_Bansall大神的英文原创作品 Calendar.get() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。