Java Date Time

Date and Time in Java

In Java, the LocalDateTime class is used to handle both date and time. The DateTimeFormatter class is used to define custom date and time formats.

The following table describes various Java DateTime formats and their results.

Format Result
LocalDateTime.now().format(DateTimeFormatter.ofPattern(“MM/dd/yyyy”)) 05/29/2015
LocalDateTime.now().format(DateTimeFormatter.ofPattern(“EEEE, dd MMMM yyyy”)) Friday, 29 May 2015
LocalDateTime.now().format(DateTimeFormatter.ofPattern(“EEEE, dd MMMM yyyy HH:mm”)) Friday, 29 May 2015 05:50
LocalDateTime.now().format(DateTimeFormatter.ofPattern(“EEEE, dd MMMM yyyy hh:mm a”)) 05/29/2015 05:50
LocalDateTime.now().format(DateTimeFormatter.ofPattern(“MM/dd/yyyy HH:mm”)) 05/29/2015 05:50 AM
LocalDateTime.now().format(DateTimeFormatter.ofPattern(“MM/dd/yyyy HH:mm”)) 05/29/2015 05:50 AM
LocalDateTime.now().format(DateTimeFormatter.ofPattern(“MM/dd/yyyy hh:mm a”)) May 29
LocalDateTime.now().format(DateTimeFormatter.ofPattern(“MMMM dd”)) 2015-05-
LocalDateTime.now().format(DateTimeFormatter.ofPattern(“yyyy-MM-dd’T’HH:mm:ss.SSSSSSS’Z'”)) 29T05:50:06.0000000Z
LocalDateTime.now().format(DateTimeFormatter.ofPattern(“EEE, dd MMM yyyy HH:mm:ss ‘GMT'”)) Fri, 29 May 2015 05:50:06 GMT
LocalDateTime.now().format(DateTimeFormatter.ofPattern(“yyyy MMMM”)) 2015 May
LocalDateTime.now().format(DateTimeFormatter.ofPattern(“HH:mm”)) 05:50
LocalDateTime.now().format(DateTimeFormatter.ofPattern(“hh:mm a”)) 05:50 AM
LocalDateTime.now().format(DateTimeFormatter.ofPattern(“HH:mm:ss”)) 05:50:06

Structure of DateTime in Java

Declaration without initialization:
LocalDateTime dateTimeVariable;
● Declaration with initialization to a specific date and time:
LocalDateTime dateTimeVariable = LocalDateTime.of(year, month, day, hour, minute, second);
● Declaration with initialization to the current date and time:
LocalDateTime currentDateTime = LocalDateTime.now();

Example:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class CurrentDateTimeExample {
public static void main(String[] args) {
// Define date time format default from system
LocalDateTime defaultDateTime = LocalDateTime.now();
// Define date time by custom format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“MM/dd/yyyy hh:mm a”);
String customDateTime = defaultDateTime.format(formatter);
System.out.println(“Current Date and Time: ” + defaultDateTime);
System.out.println(“Current Date and Time: ” + customDateTime);
}
}

● d: Day of the month as a number from 1 through 31.
● dd: Day of the month as a number with leading zero, 01 through 31.
● EEE: Abbreviated name of the day (Mon, Tues, Wed, etc).
● EEEE: Full name of the day (Monday, Tuesday, etc).
● h: 12-hour clock hour (e.g. 4).
● hh: 12-hour clock, with a leading zero (e.g. 06).
● H: 24-hour clock hour (e.g. 15).
● HH: 24-hour clock hour, with a leading zero (e.g. 22).
● m: Minutes.
● mm: Minutes with a leading zero.
● M: Month number (e.g. 3).
● MM: Month number with a leading zero (e.g. 04).
● MMM: Abbreviated Month Name (e.g. Dec).
● MMMM: Full month name (e.g. December).
● s: Seconds.
● ss: Seconds with a leading zero.
● a: AM / PM (e.g. AM or PM).
● y: Year, no leading zero (e.g. 2015 would be 15).
● yy: Year, leading zero (e.g. 2015 would be 15).
● yyyy: Year (e.g. 2015).
● z: Time zone offset from UTC, measured in hours.
● zz: Time zone offset with a leading zero.
● zzz: Time zone offset, measured in hours and minutes.
● S: Fractional seconds, 1 digit.
● SS: Fractional seconds, 2 digits.
● SSS: Fractional seconds, 3 digits.
● SSSS: Fractional seconds, 4 digits.
● SSSSS: Fractional seconds, 5 digits.
● SSSSSS: Fractional seconds, 6 digits.
● SSSSSSS: Fractional seconds, 7 digits.

Course Video

Tasks:
1. Current Date and Time:
Print the current date and time to the console using LocalDateTime.now().
2. Date Formatting:
Print the current date in the format yyyy-MM-dd (e.g., 2024-07-06) using DateTimeFormatter.
3. Date Comparison:
Create two LocalDate objects representing different dates. Compare these dates to determine which one is earlier or if they are equal.
4. Date Manipulation:
Create a LocalDate object representing today’s date. Add 5 days to this date and print the result.
5. Age Calculation:
Ask the user to enter their birthdate. Calculate their age based on the current date using Period and print it.

Frequently Asked Questions

Still have a question?

Let's talk

This free course covers Java Date and Time concepts for beginners, including practical examples.

You’ll learn how to work with Java’s date and time APIs, including classes like Date, Calendar, and the java.time package.

The key classes include Date, Calendar, LocalDate, LocalTime, LocalDateTime, and DateTimeFormatter.

The java.time package introduced in Java 8 provides modern and thread-safe classes for handling date and time.

Yes, the course covers how to use SimpleDateFormat and DateTimeFormatter for formatting dates.

The course explains how to use LocalDate.now(), LocalTime.now(), and LocalDateTime.now() to get the current date and time.

Yes, practical examples are provided for working with dates, times, and formatting.

Yes, it’s designed for beginners with clear explanations and hands-on examples.

The course covers operations like getting the current date and time, formatting, parsing, and manipulating date/time values.

Yes, it’s free. Certificates are not provided, but you’ll gain valuable skills.