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()); } } |
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
- Using Date Class
- Using get() method of the Calendar class
- Using calendar and formatter class to print the current dates in a specific format.
- Using
java.time.LocalDate
Using
java.time.LocalTime
- Using
java.time.LocalDateTime
Using java.time.Clock
Using java.sql.Date