Java Program to Convert Day to Month and Month to Day

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

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

Java Program to Convert Day to Month and Month to Day

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

Day is a period of 24 hours starting from midnight 12 AM to next 12AM which is nothing but 24 hours.

Month is also a period time used in calendar which is 30 or 31 days long.(For February 28 or 29 days(if Leap year)). There are total 12 months starting from January to December in a year.

1 Day = 0.0328767 Month
1 Month = 30.4167  Day

(For approximate result we have considered 1 month as 30.4167 days)

Formula to convert Month to Day.

Day =  Month * 30.4167

Formula to convert Day to Month.

Month = Day / 30.417

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

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

Approach:

  • Declare Day and Month value.
  • Then convert Day to Month and Month 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);
        //day value declared
        double day = 1;
        //month value declared
        double month = 1;

        //converting day to month
        double m = day / 30.417;
        //converting month to day
        double d = month * 30.417 ;
        //printing result
        System.out.println("Value of "+day+" day in month: "+ m);   
        System.out.println("Value of "+month+" month in day: "+ d);   
   }
}
Output:

Value of 1.0 day in month: 0.032876352039977644
Value of 1.0 month in day: 30.417

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

Approach:

  • Take user input of Day and Month value.
  • Then convert Day to Month and Month 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 month
        System.out.println("Enter value of month: ");  
        double month = sc.nextDouble();

        //converting day to month
        double m = day / 30.417;
        //converting month to day
        double d = month * 30.417 ;
        //printing result
        System.out.println("Value of "+day+" day in month: "+ m);   
        System.out.println("Value of "+month+" month in day: "+ d);   
   }
}
Output:

Enter value of day: 
45
Enter value of month: 
2
Value of 45.0 day in month: 1.479435841798994
Value of 2.0 month in day: 60.834

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

Approach:

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

Enter value of day: 
61
Enter value of month: 
4
Value of 61.0 day in month: 2.005457474438636
Value of 4.0 month in day: 121.668

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: