-
Hajipur, Bihar, 844101
Working with dates and time is a common requirement in programming. In Java, dates are used to store, display, and manipulate date and time information like the current time, a user’s birthday, or a timestamp for an event. Java provides several classes for handling dates — mainly Date, Calendar, and the newer LocalDate, LocalTime, and LocalDateTime classes from the java.time package.
This tutorial will help you understand how to use these classes, format dates, and perform operations such as getting the current date, adding or subtracting days, and comparing two dates.
The easiest way to get the current date and time is by using the Date class from the java.util package.
import java.util.Date;
class Example {
public static void main(String[] args) {
Date currentDate = new Date();
System.out.println("Current Date and Time: " + currentDate);
}
}
When you run this program, Java automatically shows the system’s current date and time in the default format, like:
Current Date and Time: Fri Oct 31 19:15:00 IST 2025
The Date object represents both date and time information.
If you want to display the date in a specific format (like 31/10/2025 or 2025-10-31), you can use the SimpleDateFormat class from the java.text package.
import java.text.SimpleDateFormat;
import java.util.Date;
class FormatExample {
public static void main(String[] args) {
Date currentDate = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
System.out.println("Formatted Date: " + formatter.format(currentDate));
}
}
Here,
dd stands for day,
MM for month,
yyyy for year,
HH:mm:ss for time in 24-hour format.
You can change the pattern as needed, like E, MMM dd yyyy for a more readable style (e.g., Fri, Oct 31 2025).
The java.time package was introduced in Java 8 to make date handling easier and more accurate. It includes classes like LocalDate, LocalTime, and LocalDateTime.
import java.time.LocalDate;
class Example {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
System.out.println("Current Date: " + date);
}
}
import java.time.LocalTime;
class Example {
public static void main(String[] args) {
LocalTime time = LocalTime.now();
System.out.println("Current Time: " + time);
}
}
import java.time.LocalDateTime;
class Example {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.now();
System.out.println("Current Date and Time: " + dateTime);
}
}
If you are using the java.time package, you can format date and time using the DateTimeFormatter class.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
class Example {
public static void main(String[] args) {
LocalDateTime dateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formatted = dateTime.format(formatter);
System.out.println("Formatted Date and Time: " + formatted);
}
}
This allows you to control how the date and time appear in your program output.
You can also manipulate dates by adding or subtracting time values.
import java.time.LocalDate;
class Example {
public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate nextWeek = today.plusDays(7);
LocalDate lastMonth = today.minusMonths(1);
System.out.println("Today: " + today);
System.out.println("Next Week: " + nextWeek);
System.out.println("Last Month: " + lastMonth);
}
}
This is useful when you need to find future or past dates.
To compare two dates, you can use methods like isBefore(), isAfter(), and isEqual() from the LocalDate class.
import java.time.LocalDate;
class Example {
public static void main(String[] args) {
LocalDate date1 = LocalDate.of(2025, 10, 31);
LocalDate date2 = LocalDate.of(2025, 12, 25);
if (date1.isBefore(date2)) {
System.out.println("Date1 comes before Date2");
} else if (date1.isAfter(date2)) {
System.out.println("Date1 comes after Date2");
} else {
System.out.println("Both dates are the same");
}
}
}
You can easily extract parts of a date like year, month, or day.
import java.time.LocalDate;
class Example {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
System.out.println("Year: " + date.getYear());
System.out.println("Month: " + date.getMonth());
System.out.println("Day: " + date.getDayOfMonth());
}
}
If you’re working with older Java code, you may need to convert between Date and LocalDateTime.
import java.util.Date;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
class Example {
public static void main(String[] args) {
Date date = new Date();
Instant instant = date.toInstant();
LocalDateTime localDateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
System.out.println("Converted Date: " + localDateTime);
}
}
In this tutorial, you learned how to handle date and time in Java using both the old (Date and SimpleDateFormat) and the modern (LocalDate, LocalTime, LocalDateTime, and DateTimeFormatter) approaches. The new API introduced in Java 8 is recommended because it is easier to use, immutable, and less error-prone. You also learned how to format, compare, and modify dates effectively.
Write a Java program that prints the current date and time using the Date class.
Create a Java program that displays the current date in the format dd/MM/yyyy.
Write a program that takes the current date and prints it in this format: E, MMM dd yyyy.
Create a program using the LocalDate class to display today’s date, yesterday’s date, and tomorrow’s date.
Write a Java program that adds 30 days to the current date and prints the new date.
Create a program that compares two given dates using isBefore() and isAfter() methods and prints which one is earlier.
Write a Java program that takes a date from the user in yyyy-MM-dd format and displays the month and year separately.
Create a program using DateTimeFormatter to format the current time in HH:mm:ss format.
Write a program using LocalDateTime to display the current date and time, then format it as dd-MM-yyyy HH:mm:ss.
Create a program that calculates the number of days between two given dates using ChronoUnit.DAYS.between().