2011-12-05 46 views
-1

将小时转换为时间的最佳方式是什么?将小时转换为时间java

比方说5小时,当前时间是9:17

现在,如果我运行应用程序的时间应该是0417(AM)

感谢

+0

运行应用程序=当前时间 - 小时? –

+0

你想设置系统时间吗?或者只是计算时间戳? –

+0

我不明白为什么人们将问题投下来?如果你不知道答案只是惹怒了..... – Makky

回答

4

做到这一点的序列基于当前时间:

Calendar cal = new GregorianCalendar(); 
cal.set(Calendar.HOUR_OF_DAY, 9); 
cal.set(Calendar.MINUTE, 17); 

cal.add(Calendar.HOUR_OF_DAY, -5); 
System.out.println(cal.getTime()); 
1

您可以创建一个新的DateObject这样的:

Date time5HoursBefore = new Date(System.currentTimeMillis() - 5 * 3600000); //3600000 is the number of milliseconds per hour 

另一种方法是使用Apache共享DateUtils

Date time5HoursBefore = DateUtils.addHours(new Date(), -5);