![]() |
![]() |
This topic demonstrates about how to convert given days into different forms using java program.
We will take the number of days as a input from user and calculate using simple method.
import java.util.Scanner; public class TimeConversionEx { public static void main(String[] args) { int getDays,tempDays, toDays,toWeeks,toMonths = 0,toYears; Scanner scan = new Scanner(System.in); System.out.print("Enter total Days : "); getDays = scan.nextInt(); toYears = getDays / 365; tempDays = getDays; // hold days into temp variable tempDays = tempDays - toYears * 365; toMonths = tempDays / 30; // to find months tempDays = tempDays - toMonths * 30; // to find remaining days toWeeks = tempDays / 7; tempDays = tempDays - toWeeks * 7; // to find remaining after week toDays = tempDays; System.out.println("Total entered days are : "+getDays); System.out.println("\nTotal Year(s) : "+toYears); System.out.println("Total Month(s) : "+toMonths); System.out.println("Total Week(s) : "+toWeeks); System.out.println("Total Day(s) : "+toDays); } }