Skip to content Skip to sidebar Skip to footer

Find The Difference In Time Between Two Time Values Always Gives The "Second" Value As 0

I'm trying to find the difference between current time value and a future time in HH:MM:SS format. For example: When date1 is '2017-05-11T20:30' and date2 is '2017-05-11T21:40', th

Solution 1:

You could use

difference = simpleDateFormat.parse(endTime).getTime() - new Date().getTime();

in place of these lines of your code:

String currentTime = simpleDateFormat.format(new Date());
long difference = simpleDateFormat.parse(endTime).getTime() - simpleDateFormat.parse(currentTime).getTime();

This should work fine.


Solution 2:

You can use CountDownTimer. Here is an example :

new CountDownTimer(30000, 1000) { // 30 seconds countdown
    public void onTick(long millisUntilFinished) {
        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
    }

    public void onFinish() {
        mTextField.setText("done!");
    }
}.start();

The Constructor is : CountDownTimer(long millisInFuture, long countDownInterval)


Solution 3:

You are performing a subtraction of two values and taking action if the result is greater than 0. Since it is not, it means endTime is necessarily not in the future but is before currentTime.

Fix your endTime problem.


Solution 4:

I got three suggestions.

To me the natural suggestion is you use the classes in java.time. They are much nicer to work with than the outdated Date and SimpleDateFormat that are built-in with your Android Java.

    long endMillis = LocalDateTime.parse(endTime,
                    DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm"))
            .atZone(ZoneId.systemDefault())
            .toInstant()
            .toEpochMilli();
    long difference = endMillis - System.currentTimeMillis();

The rest will be the same as in your code. To use LocalDateTime and DateTimeFormatter on Android you will need to get ThreeTenABP, it contains the classes.

I wish I could tell you to use Duration, another one of the newer classes. However, Duration doesn’t seem to lend itself well to formatting. This will change with Java 9 (not tested):

    LocalDateTime endDateTime = LocalDateTime.parse(endTime,
            DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm"));
    Duration diff = Duration.between(LocalDateTime.now(ZoneId.systemDefault()),
            endDateTime);
    if (! diff.isNegative()) {
        String hms = String.format("%02d:%02d:%02d", 
                diff.toHoursPart(),
                diff.toMinutesPart(), 
                diff.toSecondsPart());
        textView.setText(hms); //setting the remaining time in a textView
    }

Isn’t that beautiful and clear?

If you don’t want the dependency on ThreeTenABP, there is of course a fix to your code. It’s even a simplification. In your code you are formatting the new Date() that you are getting the current time from, without seconds, so they get lost, and then parsing it again, and finally getting its milliseconds since the epoch. Skip all of that and just get the current time from System.currentTimeMillis() just as in the first snippet above:

    long difference = simpleDateFormat.parse(endTime).getTime()
                        - System.currentTimeMillis();

This will give you your seconds.


Post a Comment for "Find The Difference In Time Between Two Time Values Always Gives The "Second" Value As 0"