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
import java.util.*;
public class GFG {
public static void main(String[] args)
{
Date d1 = new Date();
System.out.println( "Current date is " + d1);
}
}
|
Output
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
import java.util.*;
class GFG {
public static void main(String[] args)
{
Calendar c = Calendar.getInstance();
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));
}
}
|
Output
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
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
class GFG {
public static void main(String args[])
throws ParseException
{
SimpleDateFormat ft
= new SimpleDateFormat( "dd-MM-yyyy" );
String str = ft.format( new Date());
System.out.println( "Formatted Date : " + str);
str = "02/18/1995" ;
ft = new SimpleDateFormat( "MM/dd/yyyy" );
Date date = ft.parse(str);
System.out.println( "Parsed Date : " + date);
}
}
|
Output
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
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class DateTimeExample {
public static void main(String[] args){
LocalDate currentDate = LocalDate.now();
System.out.println( "Current date: " + currentDate);
LocalTime currentTime = LocalTime.now();
System.out.println( "Current time: " + currentTime);
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println( "Current date and time: "
+ currentDateTime);
}
}
|
Output
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
import java.time.Clock;
public class ClockExample {
public static void main(String[] args) {
Clock systemClock = Clock.systemDefaultZone();
System.out.println( "Current instant: " + systemClock.instant());
}
}
|
Output
Current instant: 2024-01-04T11:58:52.703945Z
Last Updated :
08 Mar, 2024
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...