Progress bar between two given Dates - Android Studio

Viewed 541

I want a progress bar to show the time left between given Dates. I already have put a chronometer on my app which refreshes every second and gives the time difference between two given Dates.

This is the code for calculating time between the dates below:

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

And this is the code for the updating the display so it looks like a Countdown:

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
}

How can I make a horizontal Progress Bar to show the time left between the two dates given to it. Thanks in advance.

EDIT: This is what I get using the code: debug code

2 Answers

Given the 2 dates you can calculate the difference between them as a long value:

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

Then get the currentDate always as long value:

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

Finally calculate the progress to apply to your ProgressBar:

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

and update your ProgressBar:

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

with:

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

enter image description here

Do in this way. calculate the diff between dates and div it to 100(progress bar 0..100) to get a progress bar step.

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
Related