如何计算两个时间场之间的时间差,相对于所述日期改变 [英] How to calculate the time difference between two time fields , with respect to the date changes

查看:186
本文介绍了如何计算两个时间场之间的时间差,相对于所述日期改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算的时候difference.I有三个EditTexts,我想输入的时间在HH前两个edittexts:MM格式。然后计算时间差,结果显示在相同的格式三分之一的EditText领域。 如果日期改变,时间差将根据该计算,即

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. And then calculate the time difference and the result will show on third edittext field in same format. If the date changes, the time difference will calculate according that, i.e

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

If first time = 23:00 and second time = 01:00

然后,时间差= 02:00

then, the time difference = 02:00 hours

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

    }

}

我执行这项code,但给误差源未found.I想在getDifference method.Please错误给任何其他逻辑

I executed this code ,but gives error as Source not found.I think error at getDifference method.Please give any other logic

推荐答案

时差按照code计算:

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

编辑:

date1和date2的是日期的对象,如果你有的EditText ,那么你可以将你的文字中<$通过以下code C $ C>日期对象,

date1 and date2 are Date objects, if you have EditText then you can convert your text in to Date object by using following code,

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

    }

这篇关于如何计算两个时间场之间的时间差,相对于所述日期改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆