2011-03-02 94 views
1

如何計算時差並以00:00格式顯示結果。 我找不到方法,如何將字符串數據發送到日期以及如何以EditText 00:00格式顯示結果。 我自己嘗試過,但顯示找不到源的錯誤。 以下是代碼。如何計算時差並以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); 
     } 

回答

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"; 
    } 
+0

我試了一下,它仍然顯示錯誤,我寫的代碼作爲返回DateUtils .formatElapsedTime(秒); – 2011-03-02 06:44:02

+0

什麼錯誤,你嘗試了什麼?你可以用你試過的任何方式更新你的問題嗎? – Samuh 2011-03-02 06:47:45

+0

在blockOn EditText字段上輸入數據時,它在EditText塊顯示[損壞]。我還添加了SampleDateFormat。我再次提到我的代碼,請檢查它,併爲其提供解決方案。 – 2011-03-02 09:02:34

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; 
    } 
+0

小心添加一些註釋到您的代碼? OP和任何人閱讀它將會更加清楚。 – Yaroslav 2012-10-15 08:41:16

相關問題