How to use System Clock In Java

This method we will discuss the use of clock method to fetch date and time provided by java.time package.

Java




//Java program to fetch 
//current system date and time
// using Clock
  
import java.time.Clock;
  
//Driver class
public class ClockExample {
  
      //Main method
    public static void main(String[] args) {
        // Get the default system clock
        Clock systemClock = Clock.systemDefaultZone();
  
        // Get the current instant using the clock
        System.out.println("Current instant: " + systemClock.instant());
    }
}


Output

Current instant: 2024-01-04T11:58:52.703945Z


Java – Current Date and Time

In software development, we often work with dates and times. To accurately capture the current date and time is fundamental for various applications, such as scheduling tasks, tracking events, etc.

In Java, there is a built-in class known as the Date class and we can import java.time package to work with date and time API. Here we are supposed to print the current date and time. There can be multiple ways to print the current date and time.

Different Ways To Get Current Date And Time

  1. Using Date Class
  2. Using get() method of the Calendar class
  3. Using calendar and formatter class to print the current dates in a specific format. 
  4. Using java.time.LocalDate
  5. Using java.time.LocalTime
  6. Using java.time.LocalDateTime
  7. Using java.time.Clock
  8. Using java.sql.Date

Similar Reads

Using Date Class

Using Date Class in this method we will explore the date and time module provided by java.util....

Using Calendar Instance

...

Using SimpleDateFormat

getInstance() method is generally used to get the time, date or any required some belonging to Calendar year....

Using LocalDate, LocalTime, LocalDateTime

...

Using System Clock

Using calendar and formatter class to print the current dates in a specific format....