write a program in java in which Given any date you can calculate how many days
ID: 3590010 • 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.
Explanation / Answer
using time unit class:
:
public String getTimeDiff(Date FirstDate, Date SecondDate)
{
String diff = "";
long timeDiff = Math.abs(FirstDate.getTime() - SecondDate.getTime());
diff = String.format("%d hour(s) %d min(s)", TimeUnit.MILLISECONDS.toHours(timeDiff), TimeUnit.MILLISECONDS.toMinutes(timeDiff) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(timeDiff)));
return diff;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.