Java 8 Clock millis() Method with Examples
Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8.
The millis() method of Clock class returns the current instant of the clock in milliseconds. A millisecond instant is measured from 1970-01-01T00:00Z (UTC) to the current time. This method does the same work as System.currentTimeMillis() method. If the creation of an object is unacceptable, then this method is used to allow the use of java.time.Clock in high-performance use cases.
Syntax:
public long millis()
Return Value: This method returns the current instant of the clock in milliseconds.
Exception: This method throws DateTimeException if the instant cannot be obtained from Clock.
Example::
Code: Clock clock = Clock.systemDefaultZone(); long miliSeconds=clock.millis(); System.out.println(miliSeconds); Output:: 1534749202051 Explanation:: when millis() is called, then it returns a current instant of Class Object in milliseconds.
Below programs illustrates millis() method of java.time.Clock class:
Program 1: Using millis() with Clock object created with systemDefaultZone
// Java program to demonstrate millis() // method of Clock class import java.time.*; // create class public class milisMethodDemo { // Main method public static void main(String[] args) { // create Clock Object Clock clock = Clock.systemDefaultZone(); // get Instant Object of Clock object // in miliseconds using millis() method long miliseconds = clock.millis(); // print details of miliseconds variable System.out.println("Instant for class name " + clock + " in miliseconds is " + miliseconds); } } |
Instant for class name SystemClock[Etc/UTC] in miliseconds is 1534839526984
Program 2: Using millis with Clock object created with Zone “Europe/Paris”
// Java program to demonstrate millis() // method of Clock class import java.time.*; // create class public class millisMethodDemo { // Main method public static void main(String[] args) { // create a Zone Id for Europe/Paris ZoneId zoneId = ZoneId.of("Europe/Paris"); // create Clock Object by passing zoneID Clock clock = Clock.system(zoneId); // get Instant Object of Clock object // in miliseconds using millis() method long miliseconds = clock.millis(); // print details of miliseconds variable System.out.println("Instant for clock class" + " in miliseconds is " + miliseconds); } } |
Instant for clock class in miliseconds is 1534839529510
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#millis–
Recommended Posts:
- Clock tickSeconds() method in Java with Examples
- Java 8 Clock getZone() method with Examples
- Clock systemDefaultZone() Method in Java with Examples
- Java 8 Clock equals() Method with Examples
- Clock system() Method in Java with Examples
- Clock tick() Method in Java with Examples
- Java 8 Clock fixed() method with Examples
- Java 8 Clock hashCode() method with Examples
- Clock systemUTC() Method in Java with Examples
- Java 8 Clock offset() method with Examples
- Java 8 Clock instant() method with Examples
- Java Clock tickMinutes() method in Java with Examples
- Java Clock withZone() method in Java with Examples
- Android | How to display Analog clock and Digital clock
- Java Applet | How to display a Digital Clock
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet



