How to use Date Class In Java
Using Date Class in this method we will explore the date and time module provided by java.util.
Java
// Java Program to Display Current Date and Time // Using Date class // Importing required classes import java.util.*; // Class public class GFG { // Main driver method public static void main(String[] args) { // Creating object of date class Date d1 = new Date(); // Printing the value stored in above object System.out.println( "Current date is " + d1); } } |
Current date is Thu Nov 30 07:45:38 UTC 2023
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