Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java Time Clock tick(Clock,Duration)

farenda 2016-05-30 0

java.time.Clock.tick(Clock baseClock, Duration duration)

The method Clock.tick(Clock, Duration) returns a clock that will return instants from given baseClock truncated to given duration.

Method signature

The signature of the java.time.Clock.tick(Clock, Duration) method is as follows:

public static Clock tick(
        Clock baseClock,
        Duration tickDuration)

The method is available since Java 8.

Parameters

  • Clock baseClock: not null clock to base the ticking on,
  • Duration tickDuration: not null and non-negative duration of each tick
    If tick duration is zero or one nanosecond then the base clock is returned, because such tick would have no effect.

Return value

Returns a clock that will instants of the underlying clock truncated to the nearest occurrences of given duration.

Exceptions

  • NullPointerException: if any argument is null,
  • IllegalArgumentException: if the duration is negative or has a part smaller that a millisecond such that the whole duration is not divisible into one second,
  • ArithmeticException: when the duration is too large to be represented as nanoseconds.

Example usage

In the following code we use java.time.Clock.tick(Clock, Duration):

package com.farenda.java.time;

import java.time.Clock;
import java.time.Duration;
import java.time.ZoneId;
import java.util.concurrent.TimeUnit;

import static java.time.Instant.ofEpochMilli;

public class ClockExample {

    public static void main(String[] args)
             throws InterruptedException {

        Clock fixed = Clock.fixed(
                ofEpochMilli(12345678),
                ZoneId.systemDefault());

        Clock tickClock = Clock.tick(fixed, Duration.ofSeconds(1));
        System.out.println("Tick of fixed: "
                + tickClock.millis());

        Clock sysClock = Clock.systemDefaultZone();
        System.out.println("Sys clock millis: "
                + sysClock.millis());

        tickClock = Clock.tick(sysClock, Duration.ofSeconds(1));
        System.out.println("Tick of sys clock: "
                + tickClock.millis());

        TimeUnit.SECONDS.sleep(2);
        System.out.println("Tick of sys clock: "
                + tickClock.millis());
    }
}

The above code produces the following output:

Tick of fixed: 12345000
Sys clock millis: 1464539904341
Tick of sys clock: 1464539904000
Tick of sys clock: 1464539906000

References:

  • On fixed Clocks
  • Java 8 Clock API
Share with the World!
Categories Java Tags java, java-time
Previous: Java Time Clock systemUTC
Next: Java Time Clock tickSeconds(ZoneId)

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored