Java Program to Convert Day to Year and Year to Day

In the previous article, we have discussed about Java Program to Convert Day to Month and Month to Day

In this article we will see how to convert Day to Year and Year to Day by using Java programming language.

Java Program to Convert Day to Year and Year to Day

Before jumping into the program let’s know the relationship between Day and Year and how we can convert Day to Year and vice versa.

Day is a period of 24 hours starting from midnight 12 AM to next 12AM which is nothing but 24 hours and Year is a period of time which earth takes to orbit the sun. In calendrer a year is a period of 12 months.

1 Day = 0.00273973 Year
1 Year = 365 Day

Formula to convert Year to Day.

Day = Year * 365

Formula to convert Day to Year.

Year = Day / 365

Let’s see different ways to convert Day to Year and Year to Day.

Method-1: Java Program to Convert Day to Year and Year to Day By Using Static Input Value

Approach:

  • Declare Day and Year value.
  • Then convert Day to Year and Year to Day by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //initialized value of day
        double day = 1;
        //initialized value of year
        double year = 1;
        
        //converting day to year
        double y = day / 365;
        //converting year to day
        double d =year * 365;
        //printing result
        System.out.println("Value of "+day+" day in year: "+ y);   
        System.out.println("Value of "+year+" year in day: "+ d);   
   }
}
Output:

Value of 1.0 day in year: 0.0027397260273972603
Value of 1.0 year in day: 365.0

Method-2: Java Program to Convert Day to Year and Year to Day By Using User Input Value

Approach:

  • Take user input of Day and Year value.
  • Then convert Day to Year and Year to Day by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable day
        System.out.println("Enter value of day: ");  
        double day = sc.nextDouble();
        //Taking the value input of double variable year
        System.out.println("Enter value of year: ");  
        double year = sc.nextDouble();
        
        //converting day to year
        double y = day / 365;
        //converting year to day
        double d =year * 365;
        //printing result
        System.out.println("Value of "+day+" day in year: "+ y);   
        System.out.println("Value of "+year+" year in day: "+ d);   
   }
}
Output:

Enter value of day: 
730
Enter value of year: 
4
Value of 730.0 day in year: 2.0
Value of 4.0 year in day: 1460.0

Method-3: Java Program to Convert Day to Year and Year to Day By Using User Defined Method

Approach:

  • Take user input of Day and Year value.
  • Call a user defined method by passing Day and Year value as parameter.
  • Inside method convert Day to Year and Year to Day by using the formula.
  • Print result.

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
        //Scanner class object created
        Scanner sc=new Scanner(System.in);
        //Taking the value input of double variable day
        System.out.println("Enter value of day: ");  
        double day = sc.nextDouble();
        //Taking the value input of double variable year
        System.out.println("Enter value of year: ");  
        double year = sc.nextDouble();
         //calling user defined method convert()
        convert(day, year);
   }
   
   //convert() method to convert day to year and vice versa
   public static void convert(double day, double year)
   {
        //converting day to year
        double y = day / 365;
        //converting year to day
        double d =year * 365;
        //printing result
        System.out.println("Value of "+day+" day in year: "+ y);   
        System.out.println("Value of "+year+" year in day: "+ d);   
   }
}
Output:

Enter value of day: 
700
Enter value of year: 
3.5
Value of 700.0 day in year: 1.917808219178082
Value of 3.5 year in day: 1277.5

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Related Java Programs: