get time until start of next hour in java

htt‮/:sp‬/www.lautturi.com
get time until start of next hour in java

To get the time until the start of the next hour in Java, you can use the Calendar class and the System.currentTimeMillis() method. Here's an example of how you could do this:

import java.util.Calendar;

Calendar calendar = Calendar.getInstance();
long millis = System.currentTimeMillis();
calendar.setTimeInMillis(millis);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.HOUR, 1);
long timeUntilNextHour = calendar.getTimeInMillis() - millis;
System.out.println("Time until start of next hour: " + timeUntilNextHour + " milliseconds");

This code will set the calendar to the start of the current hour, add one hour to it, and then calculate the difference between the two times in milliseconds. The result will be the time until the start of the next hour in milliseconds.

For example, if the current time is 4:34:05 PM, this code will output "Time until start of next hour: 2095000 milliseconds", which is the time until 5:00:00 PM.

Note that this code will only work if the time zone of the device is set correctly. If the time zone is not set correctly, the result may not be accurate.

Created Time:2017-11-01 12:04:59  Author:lautturi