Monday, November 18, 2024
Google search engine
HomeLanguagesJavaClock tick() Method in Java with Examples

Clock tick() Method in Java with Examples

tick(Clock baseClock, Duration tickDuration) method of java.time.Clock is a static method of Clock class that returns a clock that returns instants from the base clock rounded to the nearest occurrence of the specified duration in the parameter. The specified base clock duration must be positive, neither negative nor null. The clock will tick as per the given duration and if the duration is half of the minute or half of the second or half of the hour, the clock will return instants rounded to the half of minute or half of the second or half of the hour, as specified by the duration. 

This method can help to customize the clock class which tick according to duration. E.g. a tick means if provided the duration 5 seconds, then the second part of instant will be rounded according to 5 sec. If the base clock second part is 13, then it will be 10. Rounded clock returns instant which is smaller than the base clock. 

A duration of zero or one nanosecond would have no effect on the base clock method. These will return the same base clock. If base clock is immutable, thread-safe, and Serializable, then the returned clock will also be immutable, thread-safe, and Serializable. 

Syntax:

public static Clock tick(Clock baseClock, Duration tickDuration)

Parameters: This method takes two mandatory parameters:

  • baseClock – the base clock which you want to roundoff according to duration.
  • tickDuration – the duration of each visible tick to roundoff the clock, not negative, not null.

Return Value: This method returns a clock that uses the best available system clock in the default zone 

Exception: This method throws following exceptions:

  • IllegalArgumentException – if the duration is negative, or has a part which is very smaller than a whole millisecond such that the whole duration is not divisible into one second.
  • ArithmeticException – if the duration is too large to be represented in nanoseconds.

Example:

Code:
Clock baseClock = Clock.systemDefaultZone();
Clock clock = Clock.tick(baseClock, Duration.ofSeconds(10));

Explanation::
method tick() returns the instant which ticks after per 10 sec means the duration 
of the tick is 10sec.is instant actual time is 18:57:51.248Z then due to 10sec duration
it will round to 18:57:50Z and same for 18:59:36.247Z to 18:59:30Z.

Below programs illustrates tick() method of java.time.Clock class 

Program 1: When Clock is created with systemDefaultZone(). 

In the below program, there are three cases where duration is 30sec, 10 sec, 3sec. So tick() method has to be applied for these three cases. 

Java




// Java program to demonstrate
// tick() method of Clock class
 
import java.time.*;
 
// create class
public class tickMethodDemo {
 
    // Main method
    public static void main(String[] args)
    {
 
        // create base Clock with systemDefaultZone() method
        Clock baseclock = Clock.systemDefaultZone();
 
        // get instant of the base class and print instant
        Instant instant = baseclock.instant();
        System.out.println("Instant of Base class " + instant);
 
        // apply tick Method for Duration of 30sec and
        // create clock1 and print instant of clock1
        Clock clock1 = Clock.tick(baseclock, Duration.ofSeconds(30));
        System.out.println("Instant of Clock1 " + clock1.instant());
 
        // apply tick Method for Duration of 10sec and
        // create clock2 and print instant of clock2
        Clock clock2 = Clock.tick(baseclock, Duration.ofSeconds(10));
        System.out.println("Instant of Clock2 " + clock2.instant());
 
        // apply tick Method for Duration of 3sec and
        // create clock3 and print instant of clock3
        Clock clock3 = Clock.tick(baseclock, Duration.ofSeconds(3));
        System.out.println("Instant of Clock3 " + clock3.instant());
    }
}


Output:

Instant of Base class 2018-08-22T11:18:11.336Z
Instant of Clock1 2018-08-22T11:18:00Z
Instant of Clock2 2018-08-22T11:18:10Z
Instant of Clock3 2018-08-22T11:18:09Z

Program 2: Print the instant of the clock when the duration is in Minutes, Hours Or Days. 

Java




// Java program to demonstrate
// tick() method of Clock class
 
import java.time.*;
 
// create class
public class tickMethodDemo {
 
    // Main method
    public static void main(String[] args)
    {
 
        // create base Clock with systemUTC() method
        Clock baseclock = Clock.systemUTC();
 
        // get instant of the base class and print instant
        Instant instant = baseclock.instant();
        System.out.println("Instant of Base class "
                           + instant);
 
        // apply tick Method for Duration of 10 Minutes
        Clock clock1 = Clock.tick(baseclock,
                                  Duration.ofMinutes(10));
        System.out.println("Instant of Clock1 when duration"
                           + " = 10 minutes is "
                           + clock1.instant());
 
        // apply tick Method for Duration of 2 hours
        Clock clock2 = Clock.tick(baseclock,
                                  Duration.ofHours(2));
        System.out.println("Instant of Clock2 when duration"
                           + " = 2 hours is "
                           + clock2.instant());
 
        // apply tick Method for Duration of 5 days
        Clock clock3 = Clock.tick(baseclock,
                                  Duration.ofDays(5));
        System.out.println("Instant of Clock2 when duration"
                           + " = 5 days is "
                           + clock3.instant());
    }
}


Output:

Instant of Base class 2018-08-22T11:18:15.533Z
Instant of Clock1 when duration = 10 minutes is 2018-08-22T11:10:00Z
Instant of Clock2 when duration = 2 hours is 2018-08-22T10:00:00Z
Instant of Clock2 when duration = 5 days is 2018-08-22T00:00:00Z

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#tick-java.time.Clock-java.time.Duration-

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments