简体   繁体   English

无法显示我的 Textview1 和 Textview2 之间的计算时间差?

[英]Unable to show calculate Time difference between my Textview1 and Textview2?

在此处输入图像描述

this is the screenshot of my application the Time in which is 00:57:18 and the timeout 14:14:21 should be automatically calculated the time difference and the result will show in Total travel hours (circled with red)这是我的应用程序的屏幕截图,时间为 00:57:18 和超时 14:14:21 应自动计算时差,结果将显示在总旅行小时数中(用红色圈出)

What do you think I need to change in my codings to make the output show properly in Total Hours Difference Thank you so much.您认为我需要更改我的编码以使 output 在总时差中正确显示非常感谢。

this is the code of mine.这是我的代码。

FormTimeOut.java FormTimeOut.java

    String date_n = new SimpleDateFormat("MM/dd/yyyy", 
Locale.getDefault()).format(new Date());
    TextView date  = (TextView) findViewById(R.id.editText_timei_timedate);
    date.setText(date_n);


    DisplayDateTime = (TextView) findViewById(R.id.editText_timei_out);
    DisplayDateTimeStart = (TextView) 
findViewById(R.id.editText_travel_start);
    DisplayDateTimeTotal = (TextView) 
findViewById(R.id.editText_travel_total);


    BTN = (Button) findViewById(R.id.button1);

    calander = Calendar.getInstance();
    simpledateformat = new SimpleDateFormat("HH:mm:ss");
    Date = simpledateformat.format(calander.getTime());
    simpledateformat2 = new SimpleDateFormat("HH:mm:ss");
    Date2 = simpledateformat2.format(calander.getTime());

    BTN.setOnClickListener(new View.OnClickListener()

    {

        @Override
        public void onClick (View v){

            DisplayDateTime.setText(Date);
            DisplayDateTimeStart.setText(Date2);

            try {

                Date oldDate = (java.util.Date) 
 simpledateformat2.parse(String.valueOf(simpledateformat));


                Date currentDate = new Date();

                long diff = currentDate.getTime() - oldDate.getTime();
                long seconds = diff / 1000;
                long minutes = seconds / 60;
                long hours = minutes / 60;

                if (oldDate.before(currentDate)) {

                    DisplayDateTimeTotal.setText("hours:" + hours + "minutes:" + minutes);

                }

                // Log.e("toyBornTime", "" + toyBornTime);

            } catch (ParseException e) {

                e.printStackTrace();
            }


        }
    });

I think what you need is to show the time difference in the below text view.我认为您需要在下面的文本视图中显示时差。 Hope the below code helps to clear your doubt.希望下面的代码有助于消除您的疑问。

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");

date1 = simpleDateFormat.parse("00:57:18");
date2 = simpleDateFormat.parse("14:14:21");

long difference = date2.getTime() - date1.getTime();
hours = (int) ((difference - (1000*60*60)); 
min = (int) (difference - (1000*60*60*hours)) / (1000*60);
sec = (int) (difference - (1000*60*60*hours)) - (1000*60*min)) / (1000);
hours = (hours < 0 ? -hours : hours);
String totalTime = String.valueOf(hours) + ":" + String.valueOf(min) + ":" + String.valueOf(sec);
DisplayDateTimeTotal.setText(totalTime);

For time calculate I have used this:对于时间计算,我使用了这个:

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

you can convert your text into Date object by using the following code您可以使用以下代码将文本转换为 Date object

  DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy"); 
 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