write a program in java in which Given any date you can calculate how many days
ID: 3590025 • Letter: W
Question
write a program in java in which Given any date you can calculate how many days are left until or have passed since another date that year,
such as your birthday or another important date.
This program will ask the user to enter an important date, such as a birthday or holiday, as three separate
integer values (month, day, year) until a valid date is entered.
Then the program will ask the user to enter a second date in the same year, again as three separate integer
values (month, day, year) until a valid date is entered.
The program will then output the number of days until or since the important date, along with a conversion of
that number of days to weeks and days, to hours, and to minutes.
how do I do this without time unit class
and using only an if statements and a loop.
Explanation / Answer
DifferenceBetweenDates.java
import java.util.Scanner;
public class DifferenceBetweenDates {
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
static Scanner sc = new Scanner(System.in);
// Declaring an array and initializing corresponding days in the month
static int months[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
public static void main(String[] args) {
//Declaring variables
int diffdays = 0;
int month1, day1, year1, month2, day2, year2;
//Getting first date
System.out.println("** Date#1 **");
month1 = getMonth();
day1 = getDay(month1);
//Getting the input entered by the user
System.out.print("Enter year :");
year1 = sc.nextInt();
//Getting second date
System.out.println("** Date#2 **");
month2 = getMonth();
day2 = getDay(month2);
//Getting the input entered by the user
System.out.print("Enter year :");
year2 = sc.nextInt();
//Getting the difference between dates
diffdays = getDifferenceDaysBetMonths(month1, month2, day1, day2, year1, year2);
//displaying the output in various formats
System.out.println("Difference No of days :" + diffdays);
int week = diffdays / 7;
int day = diffdays % 7;
System.out.println("No of Weeks and Days :" + week + " weeks(s) " + day + " day(s).");
long hours = diffdays * 24;
System.out.println("No of Hours :" + hours);
long mins = diffdays * 24 * 60;
System.out.println("No of Minutes :" + mins);
}
//Finding the difference between dates taking months , days and years as inputs
private static int getDifferenceDaysBetMonths(int month1, int month2,
int day1, int day2, int year1, int year2) {
int days = 0;
if (year1 == year2) {
days = getdays(month1, month2, day1, day2);
} else if ((year2 - year1) == 1) {
days = getdays(month1, 12, day1, 31);
days += getdays(1, month2, 1, day2);
} else if ((year2 - year1) > 1) {
for (int i = year1 + 1; i < year2; i++) {
days += 365;
}
days += getdays(month1, 12, day1, 31);
days += getdays(1, month2, 1, day2);
}
return days;
}
//Finding the difference between dates taking months and days as inputs
private static int getdays(int month1, int month2, int day1, int day2) {
int diffdays = 0;
if (month1 == month2 && day1 == day2) {
diffdays = 0;
}
if ((month2 - month1) == 1) {
diffdays = months[month1 - 1] - day1 + day2;
} else if ((month2 - month1) > 1) {
diffdays = months[month1 - 1] - day1 + day2;
for (int i = month1 + 1; i < month2; i++) {
diffdays += months[i - 1];
}
}
return diffdays;
}
public static int getMonth() {
int month;
while (true) {
//Getting the input entered by the user
System.out.print("Enter month :");
month = sc.nextInt();
if (month < 1 || month > 12) {
System.out.println("** Invalid Month.Must be between 1-12 **");
continue;
} else
break;
}
return month;
}
public static int getDay(int month) {
int day;
while (true) {
//Getting the input entered by the user
System.out.print("Enter day :");
day = sc.nextInt();
if (day < 1 || day > months[month - 1]) {
System.out.println("** Invalid Month.Must be between 1-" + months[month - 1] + " **");
continue;
} else
break;
}
return day;
}
}
___________________
Output:
** Date#1 **
Enter month :2
Enter day :28
Enter year :2012
** Date#2 **
Enter month :12
Enter day :31
Enter year :2017
Difference No of days :2130
No of Weeks and Days :304 weeks(s) 2 day(s).
No of Hours :51120
No of Minutes :3067200
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.