簡體   English   中英

如何計算兩個時間字段之間的時間差,相對於日期更改

[英]How to calculate the time difference between two time fields , with respect to the date changes

我想計算時差。我有三個EditTexts,我想在HH:MM格式的前兩個edittexts中輸入時間。 然后計算時差,結果將以相同的格式顯示在第三個edittext字段上。 如果日期改變,時間差將根據該計算,即

如果第一次= 23:00,第二次= 01:00

那么,時差= 02:00小時

public class TimeCalculate extends Activity {

    private String mBlock;
    private String mBlockoff;
    private String mBlockon ;

    // String mHours, mMinutes;

    Date date1, date2;

    /** 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();

        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();
        }
        mBlock = getDifference(date1, date2);
        EditText block = (EditText) findViewById(R.id.block);
        block.setText(mBlock.toString());

    }
public static String getDifference(Date startTime, Date endTime) {

        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;
        /*int hours = (int)diff / (60 * 60 * 1000);
        int minutes = (int) (diff / (60 * 1000)); minutes = minutes - 60 * hours;
        long seconds = diff / (1000); */
        //timeDiff = DateUtils.formatElapsedTime(seconds);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:MM");
        Date date = new Date(diff);
        return simpleDateFormat.format(date);

    }

}

我執行了這段代碼,但是因為找不到Source而給出了錯誤。我認為getDifference方法的錯誤。請給出任何其他邏輯

時差通過以下代碼計算:

long difference = date2.getTime() - date1.getTime(); 
days = (int) (difference / (1000*60*60*24));  
hours = (int) ((difference - (1000*60*60*24*days)) / (1000*60*60)); 
min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours)) / (1000*60);

編輯:

date1和date2是Date對象,如果你有EditText那么你可以使用下面的代碼將你的文本轉換為Date對象,

  DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); // Make sure user insert date into edittext in this format.
     Date dateObject;

  try{
    String dob=(tx.getText().toString());
    dateObject = formatter.parse(dob);
    date = new SimpleDateFormat("dd/MM/yyyy").format(dateObject);

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM