时间格式的时间转换作为输入,移位的实时作为输出

时间:2012-02-22 09:09:51

标签: java android time timer formatting

拜托,我怎么能:

  • 输入(不使用timepicker)时移,例如02:21:22
  • 然后显示添加了时移的时钟。

因此,如果时间目前为13:20:31,则显示的时间为15:41:53 ...

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分: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);
}