DevAcademia
C++C#CPythonJava
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz
  • Java Basics

  • Java Introduction
  • Java Get Started
  • Java Syntax
  • Java Output
  • Java Comments
  • Java Variables
  • Java Data Types
  • Java Type Casting
  • Java Operators
  • Java Strings
  • Java If...Else
  • Java Switch Statement
  • Java Loops
  • Java Math
  • Java Arrays
  • Java Date
  • Java OOP

  • Java Classes/Objects
  • Java Class Attributes
  • Java Class Methods
  • Java Constructors
  • Java Destructors
  • Java this Keyword
  • Java Modifiers
  • Java Non Modifiers
  • Java Encapsulation
  • Java Packages & API
  • Java Inheritance
  • Java Polymorphism
  • Java Super Keyword
  • Java Inner Classes
  • Java Exception Handling
  • Java Abstraction
  • Java Interfaces
  • Java Enums
  • Java User Input
  • Java Quiz

  • Java Fundamentals Quiz

Loading Java tutorial…

Loading content
Java BasicsTopic 39 of 59
←PreviousPrevNextNext→

Java Date

Introduction to Date in Java

In Java, working with dates and time is essential for many applications such as logging, scheduling, and data processing.

Java provides multiple classes for handling dates and time. The most commonly used are:

- java.util.Date: A basic class to represent a specific point in time.

- java.util.Calendar: A class that provides methods for date and time manipulation.

- java.time (introduced in Java 8): A modern and recommended API that includes LocalDate, LocalTime, LocalDateTime, and ZonedDateTime.

Using java.util.Date

The Date class represents a specific instant in time, with millisecond precision.

It is often used for simple tasks like getting the current date and time.

Example
import java.util.Date;

public class DateExample {
    public static void main(String[] args) {
        Date currentDate = new Date();
        System.out.println("Current Date and Time: " + currentDate);
    }
}
Output
Current Date and Time: Wed Aug 28 21:45:12 CET 2025

Formatting Dates with SimpleDateFormat

The SimpleDateFormat class is used to format and parse dates.

You can define custom patterns like dd/MM/yyyy or yyyy-MM-dd.

Example
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateFormatExample {
    public static void main(String[] args) {
        Date currentDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        String formattedDate = formatter.format(currentDate);
        System.out.println("Formatted Date: " + formattedDate);
    }
}
Output
Formatted Date: 28/08/2025 21:45:12

Working with Calendar

The Calendar class provides more flexibility when working with dates and times.

It allows you to extract and manipulate fields such as year, month, day, hour, and minute.

Example
import java.util.Calendar;

public class CalendarExample {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        System.out.println("Year: " + cal.get(Calendar.YEAR));
        System.out.println("Month: " + (cal.get(Calendar.MONTH) + 1));
        System.out.println("Day: " + cal.get(Calendar.DAY_OF_MONTH));
    }
}
Output
Year: 2025
Month: 8
Day: 28

Modern Approach: java.time API

Since Java 8, the java.time package is the recommended way to work with dates and times.

It is more powerful, thread-safe, and less error-prone compared to Date and Calendar.

Example
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class ModernDateExample {
    public static void main(String[] args) {
        LocalDate date = LocalDate.now();
        LocalTime time = LocalTime.now();
        LocalDateTime dateTime = LocalDateTime.now();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
        String formatted = dateTime.format(formatter);

        System.out.println("Date: " + date);
        System.out.println("Time: " + time);
        System.out.println("Formatted DateTime: " + formatted);
    }
}
Output
Date: 2025-08-28
Time: 21:45:12.345
Formatted DateTime: 28-08-2025 21:45:12

Benefits of Using java.time

FeatureDescription
Thread-SafeUnlike Date and Calendar, java.time classes are immutable and safe for multi-threading.
Readable APIProvides clear and intuitive methods like plusDays(), minusMonths(), and format().
Time Zones SupportHandles time zones and offsets using ZonedDateTime.
ISO StandardBuilt on the ISO-8601 standard for consistent handling of dates and times.

Conclusion

For modern Java applications, always prefer the java.time API.

Older classes like Date and Calendar are still available for legacy support but should be avoided in new projects.

Test your knowledge: Java Date
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
Java BasicsTopic 39 of 59
←PreviousPrevNextNext→