Reputation: 310
I'm using the following package: https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html
How do I create a clock variable? I tried:
Clock clock = new Clock();
But I got the error:
TimeCheck.java:14: error: Clock is abstract; cannot be instantiated
I also tried private Clock clock; outside of the main() function, but when I ran the main() function and tried clock.instant() it said:
TimeCheck.java:16: error: non-static variable clock cannot be referenced from a static context
Upvotes: 2
Views: 690
Reputation: 338564
The java.time classes automatically use a default Clock
object.
Instant now = Instant.now() ; // Default `Clock` used implicitly.
You only need to obtain a Clock
if you want altered behavior. Typically that would be for testing or demonstration.
The java.time framework eschews the use of new
. Look for factory methods instead.
Look at the static methods on the Clock
class. You will find methods to give you various clocks with altered cadence, or fixed to a specific moment, or continually operating with a delay.
Clock fixedClock = Clock.fixed( Instant.EPOCH , ZoneId.of( "Asia/Kolkata" ) ) ;
Instant nowFake = Instant.now( fixedClock ) ;
Upvotes: 2