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
Using Date Class
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
Using Calendar Instance
getInstance() method is generally used to get the time, date or any required some belonging to Calendar year.
Tip: Whenever we require anything belonging to Calendar, Calendar class is one of naive base for sure approach to deal with date and time instances.
Java
// Java Program to Illustrate getinstance() Method // of Calendar Class // Importing required classes import java.util.*; // Class class GFG { // Main driver method public static void main(String[] args) { // Creating an object of Calendar class Calendar c = Calendar.getInstance(); // Print corresponding instances by passing // required some as in arguments System.out.println( "Day of week : " + c.get(Calendar.DAY_OF_WEEK)); System.out.println( "Day of year : " + c.get(Calendar.DAY_OF_YEAR)); System.out.println( "Week in Month : " + c.get(Calendar.WEEK_OF_MONTH)); System.out.println( "Week in Year : " + c.get(Calendar.WEEK_OF_YEAR)); System.out.println( "Day of Week in Month : " + c.get(Calendar.DAY_OF_WEEK_IN_MONTH)); System.out.println( "Hour : " + c.get(Calendar.HOUR)); System.out.println( "Minute : " + c.get(Calendar.MINUTE)); System.out.println( "Second : " + c.get(Calendar.SECOND)); System.out.println( "AM or PM : " + c.get(Calendar.AM_PM)); System.out.println( "Hour (24-hour clock) : " + c.get(Calendar.HOUR_OF_DAY)); } } |
Day of week : 5 Day of year : 334 Week in Month : 5 Week in Year : 48 Day of Week in Month : 5 Hour : 7 Minute : 45 Second : 39 AM or PM : 0 Hour (24-hour clock) : 7
Using SimpleDateFormat
Using calendar and formatter class to print the current dates in a specific format.
Java
// Java Program to Demonstrate Working of SimpleDateFormat // Class // Importing required classes import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; // Class class GFG { // Main driver method public static void main(String args[]) throws ParseException { // Formatting as per given pattern in the argument SimpleDateFormat ft = new SimpleDateFormat( "dd-MM-yyyy" ); String str = ft.format( new Date()); // Printing the formatted date System.out.println( "Formatted Date : " + str); // Parsing a custom string str = "02/18/1995" ; ft = new SimpleDateFormat( "MM/dd/yyyy" ); Date date = ft.parse(str); // Printing date as per parsed string on console System.out.println( "Parsed Date : " + date); } } |
Formatted Date : 04-01-2024 Parsed Date : Sat Feb 18 00:00:00 UTC 1995
Using LocalDate, LocalTime, LocalDateTime
In this method we will discuss various date and time methods provided by java.time which
Java
// java program to use Date and time // module in java.time package import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; // Driver class public class DateTimeExample { //Main method public static void main(String[] args){ // Current date LocalDate currentDate = LocalDate.now(); System.out.println( "Current date: " + currentDate); // Current time LocalTime currentTime = LocalTime.now(); System.out.println( "Current time: " + currentTime); // Current date and time LocalDateTime currentDateTime = LocalDateTime.now(); System.out.println( "Current date and time: " + currentDateTime); } } |
Current date: 2024-01-04 Current time: 11:59:03.285876 Current date and time: 2024-01-04T11:59:03.286975
Using System Clock
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
Contact Us