0

In an android app I have countdowntimer to display time left in game. It works fine, but when I want to get time in milliseconds for some reason it always gives me full minute.

To explain, I have Time in database and I substract from it current time, however it always gives me round number, always whole minutes so my seconds are never fine. Here is the code

setTimeLeftInMilliseconds(calendarEnd.getTimeInMillis() - Calendar.getInstance().getTimeInMillis());

calendarEnd is always the same, but I want most up to date to see how much time is left.

Example output: 429780000 429780000 429780000

Thats all for same minute but I dont get seconds

3 Answers3

3

Its simple:

Instant start = Instant.parse("2017-10-03T10:15:30.00Z");
Instant end = Instant.parse("2017-10-03T10:16:30.00Z");
        
Duration duration = Duration.between(start, end);
FikiCar
  • 425
  • 2
  • 10
1

tl;dr

java.time.Duration.between( 
    Instant.ofEpochMilli( then ) , 
    Instant.now() 
)

java.time

You are using terrible date-time classes that were supplanted by the modern java.time classes.

Apparently you want to work with a count of milliseconds since the epoch reference point of first moment of 1970 in UTC, 1970-01-01T00:00Z. For that, use Instant class.

long now = Instant.now().toEpochMilli() ;

Calculate elapsed time as count of milliseconds.

long elapsed = ( now - then ) ;

Better to work with Instant objects to determine a Duration.

Duration elapsed = Duration.between( Instant.ofEpochMilli( then ) , Instant.now() ) ;

You can ask for the entire duration as a count of milliseconds.

long elapsedMillis = elapsed.toMillis() ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

I am not sure if that is the right logic to go there. Subtraction of two Long values, in this case, time in milliseconds, is not what you should be doing. There is an option to find a difference between two dates in seconds, minutes, etc. Use this to show your time left.

Check this code, which you can simplify because you don't have to use everything in here:

DateTimeUtils obj = new DateTimeUtils();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/M/yyyy hh:mm:ss");

try {
    Date date1 = simpleDateFormat.parse("10/10/2013 11:30:10");
    Date date2 = simpleDateFormat.parse("13/10/2013 20:35:55");

    obj.printDifference(date1, date2);

} catch (ParseException e) {
    e.printStackTrace();
}

//1 minute = 60 seconds
//1 hour = 60 x 60 = 3600
//1 day = 3600 x 24 = 86400
public void 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;
    different = different % hoursInMilli;

    long elapsedMinutes = different / minutesInMilli;
    different = different % minutesInMilli;

    long elapsedSeconds = different / secondsInMilli;

    System.out.printf(
        "%d days, %d hours, %d minutes, %d seconds%n", 
        elapsedDays, elapsedHours, elapsedMinutes, elapsedSeconds);
}

Output:

startDate : Thu Oct 10 11:30:10 SGT 2013
endDate : Sun Oct 13 20:35:55 SGT 2013
different : 291945000
3 days, 9 hours, 5 minutes, 45 seconds

To get your dates you can simply use

Date endDate = simpleDateFormat.format(new Date(calendarEnd.getTimeInMillis()));
Date currentDate = simpleDateFormat.format(new Date(Calendar.getInstance().getTimeInMillis()));
SlothCoding
  • 1,546
  • 7
  • 14
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Android. For older Android look into [desugaring](https://developer.android.com/studio/write/java8-support-table) or see [How to use ThreeTenABP…](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Jan 05 '21 at 02:09