如何计算时差并以00:00格式显示结果?

时间:2011-03-02 05:17:26

标签: android android-emulator android-layout

如何计算时差并以00:00格式显示结果。 我找不到方法,如何将字符串数据发送到日期以及如何以EditText 00:00格式显示结果。 我自己试了但是它显示了找不到来源的错误。 以下是代码。

public class TimeCalculate extends Activity {

private String mBlock = null;
private String mBlockoff = null;
private String mBlockon = null;

     Date date1,date2;
     EditText block;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    EditText blockoff = (EditText) findViewById(R.id.blockoff);
    mBlockoff = blockoff.getText().toString();

    EditText blockon = (EditText) findViewById(R.id.blockon);
    mBlockon = blockon.getText().toString();
    block = (EditText) findViewById(R.id.block);
    mBlock = getDifference(date1, date2);

         date1 = new Date(mBlockoff);
    date2 = new Date(mBlockon);

              blockon.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            block = (EditText) findViewById(R.id.block);
            mBlock = getDifference(date1, date2);
            block.setText(mBlock);
        }

}

public static String getDifference(Date startTime, Date endTime) {
    String timeDiff;
    if (startTime == null)
        return "[corrupted]";
    Calendar startDateTime = Calendar.getInstance();
    startDateTime.setTime(startTime);
    Calendar endDateTime = Calendar.getInstance();
    endDateTime.setTime(endTime);
    long milliseconds1 = startDateTime.getTimeInMillis();
    long milliseconds2 = endDateTime.getTimeInMillis();
    long diff = milliseconds2 - milliseconds1;
    long hours = diff / (60 * 60 * 1000);
    long minutes = diff / (60 * 1000);
    minutes = minutes - 60 * hours;
    long seconds = diff / (1000);

    timeDiff = hours + ":" + minutes;
    return timeDiff; 
}
}

使用SampleDateFormat

修改我的代码
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm");

    try {
        date1 = simpleDateFormat.parse(mBlockoff);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        date2 = simpleDateFormat.parse(mBlockon);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

然后我按照以下方法调用该方法

blockon.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            mBlock = getDifference(date1, date2);
            block.setText(mBlock);
        }

2 个答案:

答案 0 :(得分:10)

其中一种方法可以是:
计算毫秒差异,将其转换为秒,然后使用DateUtils.elapsedTime(int sec)以hh:mm格式格式化经过时间。

请参阅DateUtilsFormatter的文档。

编辑:示例代码:此函数将以hh:mm:ss格式

返回时间
String getDifference(long now, long then){
        if(now > then)
            return DateUtils.formatElapsedTime((now - then)/1000L);
        else 
            return "error";
    }

答案 1 :(得分:2)

试试这段代码:

 public String timeDiff(int hourStart, int hourEnd, int minuteStart, int minuteEnd){    
        int hourDiff = 0;
        int minDiff = 0;
        if(hourEnd>hourStart){
            hourDiff = hourEnd-hourStart;
        }
        if(hourEnd<hourStart){
            hourEnd = hourEnd+12;
            hourStart = hourStart-12;
            hourDiff= hourEnd-hourStart;
            }
        if(minuteEnd>minuteStart){
            minDiff = minuteEnd-minuteStart;
        }
        if(minuteEnd<minuteStart){
            minuteEnd=minuteEnd+30;
            minuteStart=minuteStart-30;
            minDiff = minuteEnd-minuteStart;
        }

        return hourDiff+":"+minDiff;
    }