Compute Number of Days to Months and Days in Java GUI

This tutorial will teach you how to compute a given number of days to months and days using Java GUI. For example we input a number of days of 65, the equivalent months and days is 2 months and 5 days. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of monthsDays.java. 2. Import javax.swing package. Hence we will use a GUI (Graphical User Interface) here like the inputting the number of days.
  1. import javax.swing.*;
3. Initialize your variables in your Main, variable numInput as string and make it as an inputdialogbox. Variable months and days as integer for parsing the inputted number of days, and variable m and d as integer also for getting the equivalent months and days.
  1.         int days, months;
  2.         String numInput;
  3.         int d,m;
  4.  
  5.         numInput = JOptionPane.showInputDialog(null, "Enter number of days:");
  6.         days = Integer.parseInt(numInput);
  7.         months = Integer.parseInt(numInput);
  8.                
4. Compute the equivalent months and days by dividing the months by 30 and get the remainder of 30 in days.
  1.          d = days%30;
  2.          m = months/30;
5. Lastly, display the output of the program using JOptionPane.showMessageDialog.
  1. JOptionPane.showMessageDialog(null, "Days: " +numInput+" days = "+m+" Month and "+d+" days");
Output: output Here's the full code of this tutorial:
  1. import javax.swing.*;
  2.  
  3. public class monthsDays{
  4.  
  5.  
  6.     public static void main(String[] args) {
  7.    
  8.    
  9.         int days, months;
  10.         String numInput;
  11.         int d,m;
  12.  
  13.         numInput = JOptionPane.showInputDialog(null, "Enter number of days:");
  14.             days = Integer.parseInt(numInput);
  15.                 months = Integer.parseInt(numInput);
  16.                
  17.                  d = days%30;
  18.          m = months/30;
  19.  
  20.                
  21.        JOptionPane.showMessageDialog(null, "Days: " +numInput+" days = "+m+" Month and "+d+" days");
  22.              
  23.          }
  24.  
  25.        
  26.     }
Hope this program helps! :) Best Regards, Engr. Lyndon R. 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