[Q#5950417][A#5950446] How to increment time by 1 hour
https://stackoverflow.com/q/5950417
I have two time values. one for the previous login time and one for the current login time. I have to increase previous time login by one hour. I have used the date format hh:mm:ss. This is my code snippet. so instead of the above mentioned if condition, I have to add one hour to the previous_time and do the if condition. How to achieve this?
Answer
https://stackoverflow.com/a/5950446
APIzation
Calendar calendar = Calendar.getInstance();
calendar.setTime(previous_time);
calendar.add(Calendar.HOUR, 1);
previous_time = calendar.getTime();
// do your comparison
package com.stackoverflow.api;
import java.util.Calendar;
import java.util.Date;
public class Human5950446 {
public static Date incrementTimeByOneHour(Date previous_time) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(previous_time);
calendar.add(Calendar.HOUR, 1);
previous_time = calendar.getTime();
return previous_time;
}
}
package com.stackoverflow.api;
import java.util.Calendar;
import java.util.Date;
/**
* How to increment time by 1 hour
*
* @author APIzator
* @see <a href="https://stackoverflow.com/a/5950446">https://stackoverflow.com/a/5950446</a>
*/
public class APIzator5950446 {
public static void increment(Date previous_time) throws Exception {
Calendar calendar = Calendar.getInstance();
calendar.setTime(previous_time);
calendar.add(Calendar.HOUR, 1);
previous_time = calendar.getTime();
// do your comparison
}
}