當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。