Comments, Thoughts and Tips from the team @ Flokzu.com
Don't wanna be here? Send us removal request.
Text
Calculating days between two dates with the new Java8 time library.
With the new Java 8 time library it is very easy to calculate the difference in weeks, days, seconds, or whatever timeframe you need.
java.time.Duration.between(t1, t2).toDays();
Where t1 and t2 must be declared as java.time.Instant. Instead of toDays(); you can use toHours, toMinutes, etc.
If you are looking for the amount of days, months and years that set the two dates apart, you need to use Period.
java.time.Period.between(t1, t2);
And you can then use getDays(); getMonths() and getYears();
Note: If you use LocalDate instead of Instant, you will get an UnsupportedTemporalTypeException with the message “Unsupported unit: Seconds”, so be careful!
0 notes