Java Reference
In-Depth Information
- TimeUnit.HOURS.toMinutes(hours));
System.out.println(diff);
diff = String.format("%d days", days);
System.out.println(diff);
// Divide the number of days by seven for the weeks
int weeks = days.intValue()/7;
diff = String.format("%d weeks", weeks);
System.out.println(diff);
The output of this code will be formatted to display strings of text that indicate the
differences between the current date and the Date object that is created.
Solution #3
To determine the total cumulative difference in days, months, years, or other time unit,
use the ChronoUnit class. The following code demonstrates how to utilize the
ChronoUnit class to determine the number of days and years between two dates.
LocalDate anniversary = LocalDate.of(2000,
Month.NOVEMBER, 11);
LocalDate today = LocalDate.now();
long yearsBetween = ChronoUnit.YEARS.between(anniversary,
today);
System.out.println("Years between dates: "
+ yearsBetween);
long daysBetween = ChronoUnit.DAYS.between(anniversary,
today);
System.out.println("Days between dates:" + daysBetween);
Here are the results:
Years between dates: 13
Days between dates: 4794
Search WWH ::




Custom Search