0

Is there any method en Date or Calendar class to know the milliseconds remaining from the time of the query to a particular Date object?

I'm using Alarmmanager for reschedule the alarms and would be important for me.

The solucion that I have at the moment is get the milliseconds of the existing object and deduct the current milliseconds.

Any better solution?

Thanks!

Héctor Ortiz
  • 257
  • 1
  • 6
  • 17

3 Answers3

2

If you want how many milliseconds two Date values differ by, that's really easy using Date.getTime:

long millisLeft = target.getTime() - now.getTime();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • But... Exists any method like the following... long milis (Date objectDate) that return the milliseconds to objectDate? – Héctor Ortiz Aug 26 '12 at 18:06
  • @HéctorOrtiz: From the current time? No, but it's a one-line method to write yourself... just substitute `System.currentTimeMillis()` for `now.getTime()` above. – Jon Skeet Aug 26 '12 at 18:08
0

Yes, Just use the Calenda, This class can give you time in miiliseconds (if that is what you want. So in your case you can just subtract two seperate Calanders. by the way you might also must likly nalso need GregorianCalendar;

ie that is

 Calendar timeStamp = new GregorianCalendar();

hope this helps you can also see one of my projects that uses this at http://be.net/HARO

see the progague project in java, mostly used in the device,state device, numerical device classes.

Daniel Haro
  • 341
  • 1
  • 2
  • 11
0

tl;dr

ChronoUnit.MILLISECONDS.between(       // This enum object offers a method for calculated elapsed time in a particular granularity.
    myJavaUtilDateStart.toInstant() ,  // Convert from legacy class (`java.util.Date`) to modern class (`java.time.Instant`).
    myJavaUtilDateStop.toInstant()
)                                      // Returns a long integer.

java.time

The modern approach uses the java.time classes.

Instant

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction). This class replaces java.util.Date and java.sql.Timestamp.

Instant instant = Instant.now() ;  // Capture current moment in UTC in up to nanosecond resolution.

ChronoUnit.MILLISECONDS

To calculate elapsed time as a count of milliseconds specifically, use ChronoUnit.MILLISECONDS enum object. Beware of data loss, as any microseconds or nanoseconds in the Instant objects will be ignored.

long millisElapsed = ChronoUnit.MILLISECONDS.between( startInstant , stopInstant ) ;

Duration

Java offers a couple classes for represent a span of time unattached to the timeline:

  • Period for years-months-days.
  • Duration for hours-minutes-seconds-fractionalSecond.

Example:

Duration d = Duration.between( startInstant , stopInstant ) ;

Generate a String in standard ISO 8601 format by calling Duration::toString.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154