两个给定日期之间的进度条 - Android Studio

Progress bar between two given Dates - Android Studio

我想要一个进度条来显示给定日期之间的剩余时间。 我已经在我的应用程序上安装了一个计时器,它每秒刷新一次并给出两个给定日期之间的时差。

这是计算以下日期之间时间的代码:

public String printDifference(Date startDate, Date endDate) {
    //milliseconds
    long different = endDate.getTime() - startDate.getTime();


    System.out.println("startDate : " + startDate);
    System.out.println("endDate : "+ endDate);
    System.out.println("different : " + different);

    long secondsInMilli = 1000;
    long minutesInMilli = secondsInMilli * 60;
    long hoursInMilli = minutesInMilli * 60;
    long daysInMilli = hoursInMilli * 24;

    long elapsedDays = different / daysInMilli;
    different = different % daysInMilli;

    long elapsedHours = different / hoursInMilli;
    String hour = String.valueOf(elapsedHours);
    different = different % hoursInMilli;

    long elapsedMinutes = different / minutesInMilli;
    String mins = String.valueOf(elapsedMinutes);
    different = different % minutesInMilli;

    long elapsedSeconds = different / secondsInMilli;
    String sec = String.valueOf(elapsedSeconds);
    if(Integer.parseInt(hour)<10){
        hour = "0"+hour;
    }
    if(Integer.parseInt(mins)<10){
        mins = "0"+mins;
    }
     if(Integer.parseInt(sec)<10){
        sec = "0"+sec;
    }


    return hour + ":" + mins + ":" + sec;
}

这是更新显示的代码,看起来像倒计时:

private void updateDisplay() {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            Date curr = Calendar.getInstance().getTime();

            Calendar tmr = Calendar.getInstance();
            tmr.add(Calendar.DAY_OF_MONTH, 1);

            Date tmrTime = tmr.getTime();

            SimpleDateFormat df = new SimpleDateFormat("dd.MM.yyyy, hh:mm:ss aa", Locale.getDefault());
            SimpleDateFormat df2 = new SimpleDateFormat("dd.MM.yyyy", Locale.getDefault());



            String currentDate = df.format(curr);
            String shewanDate = df2.format(curr)+", "+shewan+":00 pm";
            String bayaniDate = df2.format(curr)+", "+bayani+":00 am";
            String bayaniDateTmr = df2.format(tmrTime)+", "+bayani+":00 am";

            Date currentD = new Date(), shewanD = new Date(), bayaniD = new Date(), bayaniTmrD = new Date();
            try {
                currentD = df.parse(currentDate);
                shewanD = df.parse(shewanDate);
                bayaniD = df.parse(bayaniDate);
                bayaniTmrD = df.parse(bayaniDateTmr);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            
            try {
                    currentD = df.parse(currentDate);
                    bayaniTmrD = df.parse(bayaniDateTmr);

                    time.setText(printDifference(currentD, bayaniTmrD));
            } catch (ParseException e) {
                e.printStackTrace();
            }


    },0,1000);//Update text every second
}

如何制作水平进度条来显示给定的两个日期之间的剩余时间。提前致谢。

编辑: 这是我使用代码得到的:

这样做。计算日期和 div 之间的差异到 100(进度条 0..100)以获得进度条步骤。

int progress = 100;
long diff = endDateValue.getTime() - startDateValue.getTime();
val step = dif/100;
val currentStateDiff = endDateValue.getTime() - new Date().getTime();
if(currentStateDiff <= 0) progress = 0 else progress = currentStateDiff / step

给定这 2 个日期,您可以将它们之间的差计算为长值:

    long startDate = ....  ;
    long endDate = .....  //calendar.getTimeInMillis();
    long diffDate = endDate - startDate;

然后获取 currentDate 始终为 long 值:

    long currentDate =  ... ;
    long currentDiff = currentDate - startDate;

最后计算应用于您的 ProgressBar 的进度:

    float progress = (float)currentDiff/diffDate*100;

并更新您的 ProgressBar:

    LinearProgressIndicator linearProgressIndicator = findViewById(R.id.progress);
    linearProgressIndicator.setProgress((int)progress);

与:

    <com.google.android.material.progressindicator.LinearProgressIndicator
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/progress"
        android:indeterminate="false"/>