当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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()函数:
示例1:

// 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

示例2:

// 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://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#getMaximum()



相关用法


注:本文由纯净天空筛选整理自RICHIK BHATTACHARJEE大神的英文原创作品 GregorianCalendar getMaximum() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。