Java Program to Convert Month to Year and Year to Month

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

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

Java Program to Convert Month to Year and Year to Month

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

Year is a period of time which earth takes to orbit the sun. In calendrer a year is a period of 12 months starting from January to December. While Month is a period time used in calendar which is 30 or 31 days long.(For February 28 or 29 days(if Leap year)).

1 Month = 0.0833334 Year
1 Year = 12 Month

Formula to convert Year to Month.

Month = Year * 12

Formula to convert Month to Year.

Year = Month / 12

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

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

Approach:

  • Declare Month and Year value.
  • Then convert Month to Year and Year to Month 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 month
        double month = 1;
        //initialized value of year
        double year = 1;
        
        //converting month to year
        double y = month / 12;
        //converting year to month
        double m =year * 12;
        //printing result
        System.out.println("Value of "+month+" month in year: "+ y);   
        System.out.println("Value of "+year+" year in month: "+ m);   
   }
}
Output:

Value of 1.0 month in year: 0.08333333333333333
Value of 1.0 year in month: 12.0

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

Approach:

  • Take user input of Month and Year value.
  • Then convert Month to Year and Year to Month 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 month
        System.out.println("Enter value of month: ");  
        double month = sc.nextDouble();
        //Taking the value input of double variable year
        System.out.println("Enter value of year: ");  
        double year = sc.nextDouble();
        
        //converting month to year
        double y = month / 12;
        //converting year to month
        double m =year * 12;
        //printing result
        System.out.println("Value of "+month+" month in year: "+ y);   
        System.out.println("Value of "+year+" year in month: "+ m);   
   }
}
Output:

Enter value of month: 
36
Enter value of year: 
4
Value of 36.0 month in year: 3.0
Value of 4.0 year in month: 48.0

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

Approach:

  • Take user input of Month and Year value.
  • Call a user defined method by passing Month and Year value as parameter.
  • Inside method convert Month to Year and Year to Month 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 month
        System.out.println("Enter value of month: ");  
        double month = 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(month, year);
   }
   
   //convert() method to convert month to year and vice versa
   public static void convert(double month, double year)
   {
        
        //converting month to year
        double y = month / 12;
        //converting year to month
        double m =year * 12;
        //printing result
        System.out.println("Value of "+month+" month in year: "+ y);   
        System.out.println("Value of "+year+" year in month: "+ m);   
   }
}
Output:

Enter value of month: 
44
Enter value of year: 
6
Value of 44.0 month in year: 3.6666666666666665
Value of 6.0 year in month: 72.0

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: