Java中的 GregorianCalendar getLeastMaximum() 方法
Java .util.GregorianCalendar.getLeastMaximum()方法是Java中的内置函数,它返回日历字段的最低最大值,作为参数传递给此GregorianCalendar 实例的函数。任何可能的时间值的最低最大值定义为 get 方法返回的最大值,其中getFirstDayOfWeek 、 getMinimalDaysInFirstWeek 、 getGregorianChange和getTimeZone方法的当前值均已考虑在内。
句法:
public int getMaximum(int calendarfield)
参数:此函数接受一个整数类型的强制参数calendarfield ,指的是函数将返回其最小值的日历字段。
返回值:此方法返回一个整数值,该整数值等于指定日历字段的最低最大值。
例子:
Input : DAY_OF_MONTH
Output : 28
Input : WEEK_OF_MONTH
Output : 3
下面的程序说明了Java.util.GregorianCalendar.getMaximum()函数的工作:
方案一:
Java
// Java program to illustrate getLeastMaximum()
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());
// Get lowest maximum for WEEK_OF_MONTH
int maxm = cal.getLeastMaximum(
GregorianCalendar.WEEK_OF_MONTH);
System.out.println("Lowest Maximum for"+
" WEEK_OF_MONTH field :" + maxm);
// Get lowest maximum for DAY_OF_MONTH
maxm = cal.getLeastMaximum(
GregorianCalendar.DAY_OF_MONTH);
System.out.println("Lowest Maximum for"+
" DAY_OF_MONTH field:" + maxm);
}
}
Java
// Java program to illustrate getLeastMaximum()
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());
// Get lowest maximum for YEAR
int maxm = cal.getLeastMaximum(
GregorianCalendar.YEAR);
System.out.println("Lowest Maximum for"+
" YEAR field :" + maxm);
// Get lowest maximum for HOUR_OF_DAY
maxm = cal.getLeastMaximum(
GregorianCalendar.HOUR_OF_DAY);
System.out.println("Lowest Maximum for"+
" HOUR_OF_DAY field:" + maxm);
}
}
输出:
Current Date and Time : Wed Aug 01 06:48:43 UTC 2018
Lowest Maximum for WEEK_OF_MONTH field :3
Lowest Maximum for DAY_OF_MONTH field:28
方案二:
Java
// Java program to illustrate getLeastMaximum()
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());
// Get lowest maximum for YEAR
int maxm = cal.getLeastMaximum(
GregorianCalendar.YEAR);
System.out.println("Lowest Maximum for"+
" YEAR field :" + maxm);
// Get lowest maximum for HOUR_OF_DAY
maxm = cal.getLeastMaximum(
GregorianCalendar.HOUR_OF_DAY);
System.out.println("Lowest Maximum for"+
" HOUR_OF_DAY field:" + maxm);
}
}
输出:
Current Date and Time : Wed Aug 01 06:48:45 UTC 2018
Lowest Maximum for YEAR field :292269054
Lowest Maximum for HOUR_OF_DAY field:23
参考: https: Java/util/GregorianCalendar.html#getLeastMaximum()