Home Java Java Date and Time

Java Date and Time

Beginner ⏱ 7 min read Updated: Jun 2026

Java provides built-in classes to work with dates and time. The Java Date and Time API allows developers to create, format, compare, and manipulate dates and times easily.

In modern Java applications, the java.time package is commonly used for handling date and time because it provides simple and powerful classes.

What is Date and Time in Java?

Date and Time in Java represent information about a specific date, time, or both date and time together. Java provides different classes to store and manage these values.

The Java Date and Time API was introduced in Java 8 to replace the older Date and Calendar classes.

📅
Key Point: The java.time package provides modern and easy-to-use classes for date and time handling.

Java Date and Time Package

Java provides the following package to work with dates and times:

Syntax

import java.time.*; 

Java Date and Time Classes

The java.time package contains several useful classes for handling different date and time requirements.

LocalDate

Used to represent a date without time information.

LocalTime

Used to represent time without date information.

LocalDateTime

Used to represent both date and time together.

DateTimeFormatter

Used to format dates and times into different patterns.

Getting Current Date in Java

The LocalDate.now() method is used to get the current date.

Example

import java.time.LocalDate; 
public class Main{
    public static void main(String[] args){ 
        LocalDate today = LocalDate.now();
        System.out.println(today);
    }
    }
Output
2026-06-20

Getting Current Time in Java

The LocalTime.now() method is used to get the current time.

Example

import java.time.LocalTime; 
public class Main{ 
    public static void main(String[] args){
        LocalTime time = LocalTime.now();
        System.out.println(time);
    }
    }
Output
14:30:45.123

Getting Current Date and Time

The LocalDateTime class is used when we need both date and time together.

Example

import java.time.LocalDateTime; 
public class Main{
    public static void main(String[] args){ 
        ocalDateTime now = LocalDateTime.now();
        System.out.println(now);
    }
    }
Output
2026-06-20T14:30:45

Formatting Date and Time in Java

Java provides the DateTimeFormatter class to display dates and times in a custom format.

Example

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main{
    public static void main(String[] args){ 
        LocalDateTime date =
        LocalDateTime.now();
        DateTimeFormatter format =
        DateTimeFormatter.ofPattern ("dd-MM-yyyy HH:mm");
        System.out.println(date.format(format));
    }
    }
Output
20-06-2026 14:30

Why Use Java Date and Time API?

  • Easy handling of dates and times.
  • Provides better accuracy and performance.
  • Supports date formatting and conversion.
  • Avoids problems of old Date and Calendar classes.

Java Date and Time Best Practices

Use java.time

Prefer modern Java Time API instead of old date classes.

Format Properly

Use DateTimeFormatter for readable date formats.

Immutable Objects

Java date and time objects are safe and immutable.