program to convert days into year month and days

Convert days into year month and days

import java.util.Scanner;
public class DateConv
{
    int Totaldays;
    int year;
    int mon
    int days;
    public static void main(System []args)
    {
         System.out.println("Enter total days to be converted months and days");
         //Creating anonymous object for the built-in class 'Scanner'
         int number= new Scanner(System.in).nextInt();
         if (number>0)
         {
               DateConv obj= new DateConv();
               obj.setTotaldays(number); //Calling setTotaldays method
               obj.Conv(); //Calling Conv method
               //Printing result
               System.out.println(obj.Totaldays+" days = "+obj.year+" year "+obj.month+" month "+obj.days+" days");    
          }
          else
                System.out.println("Please Enter valid numbers");
   
          }
          public void setTotaldays(int Totaldays)
          {
                this.Totaldays=Totaldays;
          }
          public int getTotaldays()
          {
                return this.Totaldays;
           }
           public void Conv()
           {
                 year= Totaldays/365; //Calculation
                 int rem= Totaldays%365; //declaring and initializing variable rem
                 month= rem/30; //Calculation
                 days= rem%30; //Calculation
            }
 }

Output of this program is:-

Enter total days to be converted months and days 800 800 days = 2 year 2 month 10 days

Comments

Popular posts from this blog

Java program using Scanner Class

Java Program to make a comparison between numbers

Calculate the area of Rectangle Using Constructor Overloading rules