ADVERTISEMENT
ADVERTISEMENT

Java Clock Class | tick() Method with Example

Clock Class tick() method: Here, we are going to learn about the tick() method of Clock Class with its syntax and example.
Submitted by Preeti Jain, on May 13, 2020

Clock Class tick() method

  • tick() method is available in java.time package.
  • tick() method is used to get a Clock that returns the current instant tick the given base clock (base_cl) discarded to the close occurrence of the given duration (time_du).
  • tick() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
  • tick() method does not throw an exception at the time of representing Clock.

Syntax:

    public static Clock tick(Clock base_cl, Duration time_du);

Parameter(s):

  • Clock base_cl – represents the base clock in which to add to the given duration.
  • Duration time_du – represents the duration in which to add the given clock.

Return value:

The return type of this method is Clock, it returns Clock that ticks in entire units of the given duration.

Example:

// Java program to demonstrate the example 
// of tick(Clock base_cl, Duration time_du) method of Clock

import java.time.*;

public class TickOfClock {
    public static void main(String args[]) {
        // Instantiates two ZoneId for Accra and Asmara
        ZoneId zone_1 = ZoneId.of("Africa/Accra");
        ZoneId zone_2 = ZoneId.of("Africa/Asmara");

        // Initialize two Clock objects 
        // and one Duration
        Clock cl1 = Clock.system(zone_1);
        Clock cl2 = Clock.system(zone_2);
        Duration offset = Duration.ofNanos(20000);

        // generates a new Clock from the
        // given clock(cl1) with the given duration 
        // truncated 
        Clock cl_tick = cl1.tick(cl1, offset);

        // Display cl1 and cl_tick instant
        System.out.println("cl1.instant(): " + cl1.instant());
        System.out.println("cl_tick.instant(): " + cl_tick.instant());

        System.out.println();

        // generates a new Clock from the
        // given clock(cl2) with the given duration 
        // truncated
        cl_tick = cl2.tick(cl2, offset);

        // Display cl2 and cl_tick instant
        System.out.println("cl2.instant(): " + cl2.instant());
        System.out.println("cl_tick.instant(): " + cl_tick.instant());
    }
}

Output

cl1.instant(): 2020-05-13T21:43:13.551129Z
cl_tick.instant(): 2020-05-13T21:43:13.598720Z

cl2.instant(): 2020-05-13T21:43:13.599785Z
cl_tick.instant(): 2020-05-13T21:43:13.600620Z
ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.