Issue
I'll create a count timer for my android app. I want to calculate how much time have in 24:00 in hours from current time. I couldn't not find any solutions and it's so complicated for me to calculate it with miliseconds.
For Example:
Now time is: 22.55 Have 1 hours and 5 minutes to 00.00
I'll make count timer with this like:
Remaining: 1 hours 5 Minutes
Anyone can help me? Thanks a lot.
Solution
You can use java.time.Duration to compute the time difference between now and midnight:
LocalDateTime now = LocalDateTime.now();
LocalDateTime midnight = LocalDate.now().plusDays(1).atTime(0, 0);
Duration between = Duration.between(now, midnight);
System.out.printf("Remaining: %d hours %d minutes",
between.toHours(), between.toMinutes() % 60);
Answered By - shmosel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.