📜  Java中的 GregorianCalendar getGreatestMinimum() 方法

📅  最后修改于: 2022-05-13 01:55:02.730000             🧑  作者: Mango

Java中的 GregorianCalendar getGreatestMinimum() 方法


Java .util.GregorianCalendar.getGreatestMinimum()方法是Java中的一个内置函数,它返回GregorianCalendar 实例的日历字段的最大最小值,作为参数传递给函数。任何可能的时间值的最高最小值定义为 get 方法返回的最小值,其中getFirstDayOfWeekgetMinimalDaysInFirstWeekgetGregorianChangegetTimeZone方法的当前值均已考虑在内。

句法:

public int getGreatestMinimum(int calendarfield)

参数:此函数接受一个整数类型的强制参数calendarfield ,其最大值将由函数返回。

返回值:此方法返回一个整数,表示指定日历字段的最高最小值。

例子:

Input : DAY_OF_MONTH
Output : 1

Input : WEEK_OF_MONTH
Output : 0

下面的程序说明了Java.util.GregorianCalendar.getGreatestMaximum()函数的工作原理:
方案一:

// Java Program to illustrate getGreatestMinimum()
  
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());
  
      // Greatest minimum for WEEK_OF_MONTH
      int minm = cal.getGreatestMinimum(
                   GregorianCalendar.WEEK_OF_MONTH);
      System.out.println("Greatest Minimum for"+
                   " WEEK_OF_MONTH field :" + minm);
  
      // Get greatest minimum for DAY_OF_MONTH
      minm = cal.getGreatestMinimum(
                    GregorianCalendar.DAY_OF_MONTH);
      System.out.println("Greatest Minimum for"
                    +" DAY_OF_MONTH field:" + minm);
   }
}
输出:
Current Date and Time : Wed Aug 01 07:21:14 UTC 2018
Greatest Minimum for WEEK_OF_MONTH field :0
Greatest Minimum for DAY_OF_MONTH field:1

方案二:

// Java Program to illustrate getGreatestMinimum()
  
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 greatest minimum for YEAR
      int minm = cal.getGreatestMinimum
                         (GregorianCalendar.YEAR);
      System.out.println("Greatest Minimum for"+
                           " YEAR field :" + minm);
  
      // Get greatest minimum for HOUR_OF_DAY
      minm = cal.getGreatestMinimum(
                    GregorianCalendar.HOUR_OF_DAY);
      System.out.println("Greatest Minimum for"+
                     " HOUR_OF_DAY field:" + minm);
   }
}
输出:
Current Date and Time : Wed Aug 01 07:21:16 UTC 2018
Greatest Minimum for YEAR field :1
Greatest Minimum for HOUR_OF_DAY field:0

参考: https: Java/util/GregorianCalendar.html#getGreatestMinimum()