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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。