Java program to convert the days into years, weeks, and days
ADVERTISEMENTS
In this article, you will learn how to convert the days into years, weeks, and days in the java language.
You should have the knowledge of the following topics in java programming to understand this program:
- Java
java.util.Scanner
package - Java
main()
method - Java
System.out.println()
method
Base Knowledge
1Year = 365days
1Week = 7days
Standard formula to calculate the year, weeks, and days
y = (d / 365)
w = (d % 365) / 7
d = d - ((y * 365) + (w))
Where
y = year
w = week
d = day
Source Code
// Java program to convert days into years, weeks, and days
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter the days::\n");
int d = in.nextInt();
int y;
int w;
// d = days
// y = years
// w = weeks
/* Conversion of days in to years, weeks and days */
y = (d / 365);
w = (d % 365) / 7;
d = d - ((y * 365) + (w));
// Output
System.out.println("\n" + y + " Year, " + w + " Weeks, and " + d + " Days\n");
}
}
Output
Enter the days::
464
1 Year, 14 Weeks, and 85 Days
Explanation
In this above program, we have taken an input 464
number of total days then used the standard formula to calculate the number of years, number of weeks, and number of days then It returned the following values:
- 1 Year
- 14 Weeks
- 85 Days