2012-02-22 51 views
-5

請,我怎麼可能:時間時間格式轉變爲輸入,移位實時作爲輸出

  • 輸入(不使用timepicker)時移,例如2點21分22秒
  • 然後顯示添加了時間偏移的時鐘。

所以,如果時間是當前13點20分31秒顯示的時間是十五點41分53秒......

+0

您要添加2點21分22秒與十三時20分31秒? – Rasel 2012-02-22 09:21:32

回答

0

你可能會想,如Calendar的時間增加了一些東西。例如:

Calendar future = Calendar.getInstance(); 
future.add(Calendar.HOUR_OF_DAY, enteredHour); 
future.add(Calendar.MINUTE, enteredMinute); 
future.add(Calendar.SECOND, enteredSecond); 
//And so on... 
//Now the calendar instance 'future' holds the current time plus the added values. 

至於進入時,一種可能性是進入它作爲一個文本,然後用DateFormatter從它那裏得到一個Date。例如:

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss"); 
Date enteredDate = sdf.parse(string); //String can be the input from an edit-text for example. 
//You can now get the hours/minutes/seconds from the date with any of the Dates get-methods. 
0
/** 
* This function returns the formatted string of current time, shifted by time interval, </br> 
* set in "shift" parameter, formatted in the same way. 
* @param shift 
* @return 
* @throws ParseException 
*/ 
static String addToCurrent(String shift) throws ParseException { 
    long now=System.currentTimeMillis(); 
    SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss"); 
    Date shiftDate=df.parse(shift); 
    long shiftedLong= shiftDate.getTime()+now; 
    return df.format(shiftedLong); 
}