简体   繁体   English

如何计算两个时间字段之间的时间差,相对于日期更改

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

I want to calculate the time difference.I have three EditTexts , I want to input the times in the first two edittexts in HH:MM format. 我想计算时差。我有三个EditTexts,我想在HH:MM格式的前两个edittexts中输入时间。 And then calculate the time difference and the result will show on third edittext field in same format. 然后计算时差,结果将以相同的格式显示在第三个edittext字段上。 If the date changes, the time difference will calculate according that, ie 如果日期改变,时间差将根据该计算,即

If first time = 23:00 and second time = 01:00 如果第一次= 23:00,第二次= 01:00

then, the time difference = 02:00 hours 那么,时差= 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);

    }

}

I executed this code ,but gives error as Source not found.I think error at getDifference method.Please give any other logic 我执行了这段代码,但是因为找不到Source而给出了错误。我认为getDifference方法的错误。请给出任何其他逻辑

Time Difference is calculated by following code: 时差通过以下代码计算:

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);

EDIT : 编辑:

date1 and date2 are Date objects, if you have EditText then you can convert your text in to Date object by using following code, 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