如何增加1小时的时间

And*_*lva 8 java datetime android

我有两个时间值.一个用于上一个登录时间,另一个用于当前登录时间.我必须将之前的登录时间增加一小时.我使用了日期格式hh:mm:ss.这是我的代码片段.

Date previous_time, current_time;

  if(previous_time.before(current_time)){
  Log.i("Time Comparision"," true");
 }
Run Code Online (Sandbox Code Playgroud)

所以不是上面提到的if条件,我必须向previous_time添加一个小时并执行if条件.怎么做到这一点?

pla*_*nes 25

   Calendar calendar = Calendar.getInstance();
   calendar.setTime(previous_time);
   calendar.add(Calendar.HOUR, 1);
   previous_time = calendar.getTime();
   // do your comparison
Run Code Online (Sandbox Code Playgroud)

  • @Andro_Selva - 格式化和日期的实际操作是两个独立的任务.查看[DateFormat](http://download.oracle.com/javase/6/docs/api/java/text/DateFormat.html)和/或[SimpleDateFormat](http://download.oracle.com/javase /6/docs/api/java/text/SimpleDateFormat.html). (5认同)

T-B*_*ull 5

previous_time.setTime(previous_time.getTime() + 60 * 60 * 1000);
Run Code Online (Sandbox Code Playgroud)

要么

Date session_expiry = new Date(previous_time.getTime() + 60 * 60 * 1000);
Run Code Online (Sandbox Code Playgroud)

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