2012-12-13 49 views
2

可能重複:
How to increment time by 1 hour1小時添加到當前時間的android

我使用這個代碼來獲取當前日期和時間

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd:HH:mm"); 
     String currentDateandTime = sdf.format(new Date()); 
     System.out.println("current time date" + currentDateandTime); 

現在我想爲此添加1小時。我搜查,但沒有得到我想要的答案。 現在「M越來越喜歡這個

  current time date2012:12:13:12:05 

我要回答諸如2012:在24小時格式05:12:13:13。

+1

看到這個[答案]之前(HTTP://計算器.com/a/12366336/1289716),這將幫助你確定... – MAC

回答

29

嘗試爲:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy:MM:dd:HH:mm"); 
String currentDateandTime = sdf.format(new Date()); 

Date date = formatter.parse(currentDateandTime); 
Calendar calendar = Calendar.getInstance(); 
calendar.setTime(date); 
calendar.add(Calendar.HOUR, 1); 

System.out.println("Time here "+calendar.getTime()); 
12

可以增加時間像這樣把它傳遞給sdf.format(日期)

Date a=new Date(); 
    a.setTime(System.currentTimeMillis()+(60*60*1000)); 
5
Calendar now = Calendar.getInstance(); 
now.add(Calendar.HOUR,1); 
System.out.println(now.getTime());