Date, DateFormat and Calendar Class in Java
Posted by Raghuveer Singh
Posted on 14th Apr 2026 7:49 PM
( 30 min Read & 40 min Implementation )

#java #date #date-format #calendar
Article Outline

Why Predefined Date classes is important ?


Handling date and time is a fundamental requirement in many Java applications such as scheduling systems, logging and data processing. Java provides several classes to work with date and time including Date,DateFormat and Calendar. This blog explains these classes with example and highlights of their usage.



Date Class


The date class from java.util package represents a specific moment in time, measured in milliseconds since January1, 1970.


Example:

import java.util.Date;

public class Main {
public static void main(String[] args) {
Date currentDate = new Date();
System.out.println(currentDate);
}
}


Output:

Wed Apr 02 22:50:45 IST 2026



Year in Date Class

year can be retrieved using the getYear() method, but it returns the value as the number of years since 1900. This means you must add 1900 to get the actual year. Although deprecated, it is still used in some legacy systems where migration is not feasible. Example:


import java.util.Date;

Date date = new Date();
int year = date.getYear() + 1900;

System.out.println("Year: " + year);



Month in Date Class

month can be obtained using the getMonth() method, which returns a 0-based value (0 for January, 11 for December). To convert it into a human-readable format, you need to add 1 to the result. This behavior often leads to bugs if not handled carefully.


import java.util.Date;

Date date = new Date();
int month = date.getMonth() + 1;

System.out.println("Month: " + month);



Date in Date Class


day of the month, the getDate() method is used, which directly returns the correct day (1–31) without requiring any adjustment. Even though this method is also deprecated, it is straightforward compared to year and month extraction


import java.util.Date;

Date date = new Date();
int day = date.getDate();

System.out.println("Day: " + day);



Key Points:

• It stores date and time information.

• It is mostly used for timestamps.



DateFormat Class


The DateFormat class is used to format and parse dates. It is an abstract class, commonly implemented using SimpleDateFormat.


Formatting Date:

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

public class Main {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy-HH-mm-ss");

String formattedDate = formatter.format(date);
System.out.println(formattedDate);
}
}


Output:

02-04-2026-23-30-45


List of all Letters in Date Format



Example with Date Format




Calendar Class


The Calendar class is used for manipulating dates and extracting specific components like year, month and day.


Getting Current Date Information:

import java.util.Calendar;

public class Main {
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: 2026
Month: 4
Day: 2

Key Point:

• Why +1 in month? - In Java’s Calendar class, months are zero-based, Means they start from 0.


Adding Days:


import java.util.Calendar;

public class Main {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 5);

System.out.println("After 5 Days The Date And Time Will Be: "+ cal.getTime());
}
}

Output:

After 5 Days The Date And Time Will Be: Tue Apr 07 23:30:45 IST 2026


Setting a Specific Date:


import java.util.Calendar;

public class Main {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
cal.set(2026, Calendar.APRIL, 10);

System.out.println(cal.getTime());
}
}


Output:

Fri Apr 10 23:30:45 IST 2026


Modern Alternative:


Java 8 introduced the java.time package, which provides a more efficient and user friendly way to handle date and time.


Example:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
public static void main(String[] args) {

LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy-HH-mm-ss");

System.out.println(now.format(formatter));
}
}


Output:

02-04-2026-23-30-45



Conclusion:


The Date, DateFormat, Calendar classes are used in Java to handle date and time operations. The Date class represents a specific moment in time but has limited functionality and many deprecated methods. The DateFormat class is used to format and parse dates into readable strings. The Calendar class provides more control and allows manipulation of dates such as adding or retrieving specific fields like day, month and year. However these classes are part of the older Java API, and the modern java.time package is now preferred for better performance, clarity and ease of use.


All Comments ()
Do You want to add Comment in this Blog? Please Login ?