0

How can i calcutate the second that are between a currentTime and another date?

long time = myData - SystemClock.elapsedRealtime();
user2520969
  • 1,389
  • 6
  • 20
  • 30

3 Answers3

2

You use System.currentTimeMillis() to get current. Then use System.currentTimeMillis() again on the time of offset. Then use the latest one and subtract the first one.

SystemClock.elapsedRealtime(); returns time since bootup. not a speific time.

wtsang02
  • 18,603
  • 10
  • 49
  • 67
  • So i can do long time = object.getData - System.currentTimeMillis() ?? – user2520969 Jul 11 '13 at 18:54
  • if object.getData returns the time in milliseconds of the date you want. yes. – wtsang02 Jul 11 '13 at 18:55
  • No, it return gregorian calendar. What thing i change? – user2520969 Jul 11 '13 at 18:57
  • Please make your question more clear. What is object.getData, what are your output atm? – wtsang02 Jul 11 '13 at 19:01
  • Ok sorry. I have an object composed by some variable and a gregorian calendar, so when i do getData() it return for example 4-10-2013. So i must calculate the millisecond that are between that data and the currentTime. – user2520969 Jul 11 '13 at 19:07
  • Ok in this case. get your gregorian calendar object as `calendar`. then use `calendar.get(Calendar.MILLISECOND))` , to get the milliseconds of that moment. then use `TheMilliSecYouJustGot-System.currentTimeMillis() ` to get the difference. – wtsang02 Jul 11 '13 at 19:23
1

java.time

The legacy date-time API (java.util date-time types and their formatting API, SimpleDateFormat) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time, the modern date-time API*.

Solution using the modern API:

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.GregorianCalendar;

public class Main {
    public static void main(String[] args) {
        // An arbitrary GregorianCalendar for demo - you will get it from object.getData
        GregorianCalendar gcal = GregorianCalendar
                .from(ZonedDateTime.of(2021, 5, 2, 10, 20, 30, 0, ZoneId.systemDefault()));

        ZonedDateTime start = gcal.toZonedDateTime();
        ZonedDateTime current = ZonedDateTime.now(ZoneId.systemDefault());

        long seconds = ChronoUnit.SECONDS.between(start, current);

        System.out.println(seconds);
    }
}

Output:

353263

Note: In the code given above, I have used ZoneId.systemDefault() which returns the ZoneId of your JVM. Replace it with the applicable ZoneId e.g. ZoneId.of("Europe/London").

Learn more about the the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

Joda-Time

FYI, Joda-Time offers a Seconds class.

long secs = Seconds.between( new DateTime( myGregCal.getTime() ), DateTime.now() ).getSeconds();
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154