How to Check if an Inputted Year is a Leap Year using Java GUI

In this tutorial, i will teach you how to check if an inputted year as leap year or not. Hence we already know that leap year has 366 days instead of the normal 365 days. Leap years occur every 4 years. And this fact is our formula in finding a leap year using Java GUI. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of leapYear.java. 2. Import javax.swing package. Hence we will use a GUI (Graphical User Interface) here like the inputting the year. Import also java.util package to access the Gregorian Calendar class. 3. Initialize your variables in your Main, variable numInput as string and make it as an inputdialogbox. Variable yr as integer that will parse the inputted value of numInput for inputting the year.
  1.         int yr;
  2.         String numInput;       
  3.         numInput = JOptionPane.showInputDialog(null, "Enter year:");
  4.             yr = Integer.parseInt(numInput);
4. Now, let's obtain an instance of GregorianCalendar variable of calendar. This will trigger to access the package of java.util in the calendar class of it. 5. To find the Leap year, we will create an If-Else statement. In our if statement we will use the isLeapYear method of our Gregorian Calendar class that we have instantiated earlier. Otherwise, in Else statment, it will prompt not a leap year.
  1.                 if (calendar.isLeapYear(yr))
  2.         {        
  3.            JOptionPane.showMessageDialog(null, "The year " +yr + " is a Leap Year");
  4.         }
  5.         else{
  6.                 JOptionPane.showMessageDialog(null, "The year " +yr + " is not a Leap Year");
  7.         }
  8.        
Output: output Here's the full source code:
  1. import javax.swing.*;
  2. import java.util.*;
  3. public class leapYear{
  4.  
  5.  
  6.     public static void main(String[] args) {
  7.         int yr;
  8.         String numInput;  
  9.                
  10.         numInput = JOptionPane.showInputDialog(null, "Enter year:");
  11.             yr = Integer.parseInt(numInput);
  12.            
  13.              GregorianCalendar calendar = new GregorianCalendar();
  14.                
  15.                
  16.                 if (calendar.isLeapYear(yr))
  17.         {        
  18.            JOptionPane.showMessageDialog(null, "The year " +yr + " is a Leap Year");
  19.         }
  20.         else{
  21.                 JOptionPane.showMessageDialog(null, "The year " +yr + " is not a Leap Year");
  22.         }
  23.        
  24.              
  25.          }    
  26.     }
Hope this helps! :) Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment