Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increment time by 1 hour

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.

Date previous_time, current_time;

  if(previous_time.before(current_time)){
  Log.i("Time Comparision"," true");
 }

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?

like image 437
Andro Selva Avatar asked May 10 '11 13:05

Andro Selva


3 Answers

   Calendar calendar = Calendar.getInstance();
   calendar.setTime(previous_time);
   calendar.add(Calendar.HOUR, 1);
   previous_time = calendar.getTime();
   // do your comparison
like image 137
planetjones Avatar answered Oct 11 '22 11:10

planetjones


previous_time.setTime(previous_time.getTime() + 60 * 60 * 1000);

or

Date session_expiry = new Date(previous_time.getTime() + 60 * 60 * 1000);

http://download.oracle.com/javase/6/docs/api/java/util/Date.html#getTime%28%29

like image 44
T-Bull Avatar answered Oct 11 '22 12:10

T-Bull


Please try this code.

    SimpleDateFormat sdf = new SimpleDateFormat("h:mm a");
    Date date = Utils.getBookingDate(mBooking.ToTime);
    Calendar calendarAdd = Calendar.getInstance();
    calendarAdd.setTime(date);
    calendarAdd.add(Calendar.HOUR, 1);
    toTime = sdf.format(calendarAdd.getTime());
    tv_Totime.setText(toTime);

when current time string formate within add 1 hours

like image 31
Maulik Gohel Avatar answered Oct 11 '22 12:10

Maulik Gohel