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


Java GregorianCalendar isLeapYear()用法及代碼示例


java.util.GregorianCalendar.isLeapYear()方法確定作為參數傳遞給函數的給定年份是否為leap年,如果給定年份為a年,則返回true,否則返回false。

用法:

public boolean isLeapYear(int year)

參數:該函數接受單個整數參數year,該年份表示該函數需要檢查是否為a年的年份。

返回值:該函數返回一個布爾值。如果作為參數傳遞的年份是a年,則返回true和false。


例子:

Input : 2016
Output : true

Input : 2018
Output : false

以下示例程序旨在說明Java中的java.util.GregorianCalendar.isLeapYear()函數:

示例1:

// Java Program to illustrate isLeapYear() function  
// of GregorianCalendar 
  
import java.io.*; 
import java.util.*; 
  
class GFG { 
     public static void main(String[] args) { 
      
      // Create a new calendar 
      GregorianCalendar c = (GregorianCalendar)  
                 GregorianCalendar.getInstance(); 
  
      // Display the current date and time 
      System.out.println("Current Date and Time : "
                                 + c.getTime()); 
  
      int year = c.get(GregorianCalendar.YEAR); 
      if(c.isLeapYear(year)) 
      { 
           System.out.println(year +  
                          " is leap year"); 
      } 
      else
      { 
          System.out.println(year +  
                     " is Not a leap year"); 
      } 
   } 
}
輸出:
Current Date and Time : Fri Jul 27 11:53:39 UTC 2018
2018 is Not a leap year

示例2:

// Java Program to illustrate isLeapYear() function  
// of GregorianCalendar 
  
import java.io.*; 
import java.util.*; 
  
class GFG { 
     public static void main(String[] args) { 
      
      // Create a new calendar 
      GregorianCalendar c = (GregorianCalendar)  
                GregorianCalendar.getInstance(); 
  
      // Display the current date and time 
      System.out.println("" + c.getTime()); 
        
      // Modifying the current calender 
      c.add((GregorianCalendar.MONTH), -30); 
  
      int year = c.get(GregorianCalendar.YEAR); 
      if(c.isLeapYear(year)) 
      { 
           System.out.println(year + " is leap year"); 
      } 
      else
      { 
          System.out.println(year + " is Not a leap year"); 
      } 
   } 
}
輸出:
Fri Jul 27 11:53:41 UTC 2018
2016 is leap year

參考:https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html#isLeapYear()



相關用法


注:本文由純淨天空篩選整理自RICHIK BHATTACHARJEE大神的英文原創作品 GregorianCalendar isLeapYear() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。