JavaDate and Time

Java Date and Time

In Java, working with dates and times is easy using the Date class, which is part of the java.util package. This class represents both the current date and time and allows you to perform various date and time operations.

Constructors of Date Class

Sr.No.

Constructor

Description

1

Date()

Initializes a new Date object with the current date and time.

2

Date(long millisec)

Creates a Date object using milliseconds since January 1, 1970, 00:00:00 GMT (the epoch).

Commonly Used Methods of the Date Class

Sr.No.

Method

Description

1

boolean after(Date when)

Checks if this date comes after the specified date.

2

boolean before(Date when)

Checks if this date comes before the specified date.

3

Object clone()

Returns a copy of the date object.

4

int compareTo(Date anotherDate)

Compares two date objects for order.

5

boolean equals(Object obj)

Checks whether two dates are equal.

6

static Date from(Instant instant)

Creates a Date object from an Instant object.

7

long getTime()

Returns the number of milliseconds since the epoch.

8

void setTime(long time)

Sets the date and time using the given milliseconds since epoch.

9

Instant toInstant()

Converts this Date object into an Instant.

10

String toString()

Converts the date into a readable string format.

Get Current Date and Time in Java

Java
import java.util.Date;

public class DateDemo {
   public static void main(String[] args) {
      // Creating Date object
      Date date = new Date();

      // Displaying current date and time
      System.out.println("Current Date and Time: " + date.toString());
   }
}
Current Date and Time: Sun May 04 09:51:52 CDT 2009
Comparing Two Dates

You can compare dates in Java in the following ways:

  • Using getTime() — Compares milliseconds between two dates.

  • Using before(), after(), and equals() — Checks relational differences.

  • Using compareTo() — Returns a positive, negative, or zero value.

Date Formatting with SimpleDateFormat

The SimpleDateFormat class (from java.text package) is used to format and parse dates according to custom patterns.

Java
import java.util.*;
import java.text.*;

public class DateFormatExample {
   public static void main(String[] args) {
      Date now = new Date();
      SimpleDateFormat format = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

      System.out.println("Formatted Date: " + format.format(now));
   }
}
Formatted Date: Sun 2004.07.18 at 04:14:09 PM PDT
Common Format Patterns

Symbol

Description

Example

G

Era

AD

y

Year

2025

M

Month

10 or October

d

Day in month

12

h

Hour (1–12)

11

H

Hour (0–23)

23

m

Minute

59

s

Second

45

a

AM/PM marker

PM

z

Time zone

IST

Date Formatting Using printf()

Java
import java.util.Date;

public class PrintfDateExample {
   public static void main(String[] args) {
      Date date = new Date();
      System.out.printf("Current Date and Time: %tc", date);
   }
}
Current Date and Time: Sat Dec 15 16:37:57 IST 2025
Parsing String into Date

Java
import java.util.*;
import java.text.*;

public class ParseDateExample {
   public static void main(String[] args) {
      SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
      String input = "2025-10-12";

      try {
         Date t = ft.parse(input);
         System.out.println(input + " Parses as: " + t);
      } catch (ParseException e) {
         System.out.println("Unable to parse: " + input);
      }
   }
}
2025-10-12 Parses as: Sun Oct 12 00:00:00 IST 2025
Sleeping the Program

Java
import java.util.*;

public class SleepExample {
   public static void main(String[] args) {
      try {
         System.out.println(new Date());
         Thread.sleep(3000); // 3 seconds
         System.out.println(new Date());
      } catch (Exception e) {
         System.out.println("An error occurred!");
      }
   }
}
Sun May 03 18:04:41 IST 2025
Sun May 03 18:04:44 IST 2025
Measuring Elapsed Time

Java
import java.util.*;

public class TimeDifferenceExample {
   public static void main(String[] args) {
      try {
         long start = System.currentTimeMillis();
         Thread.sleep(2000); // 2 seconds delay
         long end = System.currentTimeMillis();
         long diff = end - start;
         System.out.println("Elapsed Time: " + diff + " milliseconds");
      } catch (Exception e) {
         System.out.println("Exception occurred!");
      }
   }
}
GregorianCalendar Class

The GregorianCalendar class is a concrete implementation of the Calendar class. It represents the Gregorian calendar — the calendar most commonly used worldwide.

Java
import java.util.*;

public class GregorianCalendarExample {
   public static void main(String[] args) {
      String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
                         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

      GregorianCalendar gcal = new GregorianCalendar();
      int year = gcal.get(Calendar.YEAR);

      System.out.println("Date: " + months[gcal.get(Calendar.MONTH)] + " " +
                         gcal.get(Calendar.DATE) + " " + year);
      System.out.println("Time: " + gcal.get(Calendar.HOUR) + ":" +
                         gcal.get(Calendar.MINUTE) + ":" +
                         gcal.get(Calendar.SECOND));

      if (gcal.isLeapYear(year)) {
         System.out.println("This year is a leap year.");
      } else {
         System.out.println("This year is not a leap year.");
      }
   }
}
Success
In short: Use Date for simple date/time handling. Use SimpleDateFormat for formatting and parsing. Use GregorianCalendar for advanced calendar operations.