Java中的 GregorianCalendar getMaximum() 方法
Java .util.GregorianCalendar.getMaximum()方法是Java中的一个内置函数,它返回作为参数传递给函数的特定日历字段的最大值。对于任何可能的时间值,它是 get() 方法返回的最大值,同时考虑 getFirstDayOfWeek()、getMinimalDaysInFirstWeek()、getGregorianChange() 和 getTimeZone() 方法的当前值。
句法:
public int getMaximum(int calendarfield)
参数:此函数接受一个强制参数calendarfield ,该GregorianCalender 实例的最大值将由函数返回。
返回值:此方法返回此GregorianCalender 实例的指定日历字段的最大值。
例子:
Input : DAY_OF_MONTH
Output : 31
Input : WEEK_OF_MONTH
Output : 6
下面的程序说明了Java.util.GregorianCalendar.getMaximum()函数:
方案一:
// Java Program to illustrate getMaximum() function
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Create a new calendar
GregorianCalendar cal = (GregorianCalendar)
GregorianCalendar.getInstance();
// Display the current date and time
System.out.println("Current Date and Time : "
+ cal.getTime());
// Display maximum for WEEK_OF_MONTH
int maxm = cal.getMaximum
(GregorianCalendar.WEEK_OF_MONTH);
System.out.println("Maximum for WEEK_OF_MONTH field :"
+ maxm);
// Display maximum for DAY_OF_MONTH
maxm = cal.getMaximum
(GregorianCalendar.DAY_OF_MONTH);
System.out.println("Maximum for DAY_OF_MONTH field:"
+ maxm);
}
}
输出:
Current Date and Time : Fri Jul 27 12:44:38 UTC 2018
Maximum for WEEK_OF_MONTH field :6
Maximum for DAY_OF_MONTH field:31
方案二:
// Java Program to illustrate getMaximum() function
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
// Create a new calendar
GregorianCalendar cal = (GregorianCalendar)
GregorianCalendar.getInstance();
// Display the current date and time
System.out.println("Current Date and Time : "
+ cal.getTime());
// Display maximum for YEAR
int maxm = cal.getMaximum(GregorianCalendar.YEAR);
System.out.println("Maximum for YEAR field :"
+ maxm);
// Display maximum for HOUR_OF_DAY
maxm = cal.getMaximum(GregorianCalendar.HOUR_OF_DAY);
System.out.println("Maximum for HOUR_OF_DAY field:"
+ maxm);
}
}
输出:
Current Date and Time : Fri Jul 27 12:44:40 UTC 2018
Maximum for YEAR field :292278994
Maximum for HOUR_OF_DAY field:23
参考: https: Java/util/GregorianCalendar.html#getMaximum()