TL;DR
Duration.between( objectBorn , Instant.now() )
java.time
lakmal , , , , java.time.
, Java . .
Instant
Instant UTC ( (9) ).
Instant instant = Instant.now(); // Current moment in UTC.
Java 8, ( , ). Java 9 Clock , . , Java 8, Java 9 ; Java 8.
Duration
Duration .
.
package javatimestuff;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
public class Life {
private Instant objectBorn = null;
public Life () {
this.objectBorn = Instant.now ();
}
public Duration objectAgeAsDuration () {
Duration d = Duration.between ( objectBorn , Instant.now () );
return d;
}
public long objectAgeAsNanos () {
Duration d = this.objectAgeAsDuration ();
return d.toNanos ();
}
}
.
public static void main ( String[] args ) {
System.out.println ( "Please wait a few seconds as we birth a new `Life` object. " );
Life l = new Life ();
try {
Thread.sleep ( TimeUnit.SECONDS.toMillis ( 3 ) );
} catch ( InterruptedException ex ) {
}
System.out.println ( "The Life objectās age as Duration: " + l.objectAgeAsDuration () );
System.out.println ( "The Life objectās age as nanoseconds: " + l.objectAgeAsNanos () );
}
, Life.
: PT3.002S
Life Life : 3010000000
finalize
finalize. :
finalize , , . , . - Java, Java.finalize . , JVM , , finalize .
, finalize . (teardown, shutdown, , ), , .
Object::finalize() Java 9 . :
. . , . ; , ; . , . finalize , , , . , , , , AutoCloseable. Cleaner PhantomReference , .
java.time
java.time Java 8 . legacy , java.util.Date, Calendar SimpleDateFormat.
Joda-Time, , java.time.
, . Oracle. Qaru . JSR 310.
java.time?
ThreeTen-Extra java.time . java.time. , Interval, YearWeek, YearQuarter .