Java LocalDateTime API Guide

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

The LocalDateTime class in Java 8 represents a date-time without a time-zone in the ISO-8601 calendar system. It is a combination of LocalDate and LocalTime. This guide covers the essential methods provided by the LocalDateTime class, demonstrating how to create, manipulate, and query date-time objects.

1. now()

Obtains the current date-time from the system clock in the default time-zone.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime currentDateTime = LocalDateTime.now();
        System.out.println("Current Date-Time: " + currentDateTime);
    }
}

Output:

Current Date-Time: 2024-07-05T13:58:37.108724

2. of(int year, int month, int dayOfMonth, int hour, int minute)

Obtains an instance of LocalDateTime from a year, month, day, hour, and minute.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.of(2024, 7, 5, 10, 15);
        System.out.println("Specific Date-Time: " + dateTime);
    }
}

Output:

Specific Date-Time: 2024-07-05T10:15

3. of(int year, int month, int dayOfMonth, int hour, int minute, int second)

Obtains an instance of LocalDateTime from a year, month, day, hour, minute, and second.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.of(2024, 7, 5, 10, 15, 30);
        System.out.println("Specific Date-Time: " + dateTime);
    }
}

Output:

Specific Date-Time: 2024-07-05T10:15:30

4. parse(CharSequence text)

Obtains an instance of LocalDateTime from a text string such as 2007-12-03T10:15:30.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.parse("2024-07-05T10:15:30");
        System.out.println("Parsed Date-Time: " + dateTime);
    }
}

Output:

Parsed Date-Time: 2024-07-05T10:15:30

5. plusYears(long years)

Returns a copy of this LocalDateTime with the specified number of years added.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDateTime newDateTime = dateTime.plusYears(2);
        System.out.println("Date-Time plus 2 years: " + newDateTime);
    }
}

Output:

Date-Time plus 2 years: 2026-07-05T13:58:37.489840

6. plusMonths(long months)

Returns a copy of this LocalDateTime with the specified number of months added.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDateTime newDateTime = dateTime.plusMonths(3);
        System.out.println("Date-Time plus 3 months: " + newDateTime);
    }
}

Output:

Date-Time plus 3 months: 2024-10-05T13:58:37.596334

7. plusDays(long days)

Returns a copy of this LocalDateTime with the specified number of days added.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDateTime newDateTime = dateTime.plusDays(10);
        System.out.println("Date-Time plus 10 days: " + newDateTime);
    }
}

Output:

Date-Time plus 10 days: 2024-07-15T13:58:37.692432

8. plusHours(long hours)

Returns a copy of this LocalDateTime with the specified number of hours added.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDateTime newDateTime = dateTime.plusHours(5);
        System.out.println("Date-Time plus 5 hours: " + newDateTime);
    }
}

Output:

Date-Time plus 5 hours: 2024-07-05T18:58:37.790362

9. plusMinutes(long minutes)

Returns a copy of this LocalDateTime with the specified number of minutes added.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDateTime newDateTime = dateTime.plusMinutes(30);
        System.out.println("Date-Time plus 30 minutes: " + newDateTime);
    }
}

Output:

Date-Time plus 30 minutes: 2024-07-05T14:28:37.890650

10. plusSeconds(long seconds)

Returns a copy of this LocalDateTime with the specified number of seconds added.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDateTime newDateTime = dateTime.plusSeconds(45);
        System.out.println("Date-Time plus 45 seconds: " + newDateTime);
    }
}

Output:

Date-Time plus 45 seconds: 2024-07-05T13:59:22.991715

11. minusYears(long years)

Returns a copy of this LocalDateTime with the specified number of years subtracted.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDateTime newDateTime = dateTime.minusYears(1);
        System.out.println("Date-Time minus 1 year: " + newDateTime);
    }
}

Output:

Date-Time minus 1 year: 2023-07-05T13:58:38.094711

12. minusMonths(long months)

Returns a copy of this LocalDateTime with the specified number of months subtracted.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDateTime newDateTime = dateTime.minusMonths(2);
        System.out.println("Date-Time minus 2 months: " + newDateTime);
    }
}

Output:

Date-Time minus 2 months: 2024-05-05T13:58:38.175919

13. minusDays(long days)

Returns a copy of this LocalDateTime with the specified number of days subtracted.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDateTime newDateTime = dateTime.minusDays(5);
        System.out.println("Date-Time minus 5 days: " + newDateTime);
    }
}

Output:

Date-Time minus 5 days: 2024-06-30T13:58:38.269588

14. minusHours(long hours)

Returns a copy of this LocalDateTime with the specified number of hours subtracted.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDateTime newDateTime = dateTime.minusHours(3);
        System.out.println("Date-Time minus 3 hours: " + newDateTime);
    }
}

Output:

Date-Time minus 3 hours: 2024-07-05T10:58:38.371307

15. minusMinutes(long minutes)

Returns a copy of this LocalDateTime with

the specified number of minutes subtracted.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDateTime newDateTime = dateTime.minusMinutes(15);
        System.out.println("Date-Time minus 15 minutes: " + newDateTime);
    }
}

Output:

Date-Time minus 15 minutes: 2024-07-05T13:43:38.467557

16. minusSeconds(long seconds)

Returns a copy of this LocalDateTime with the specified number of seconds subtracted.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDateTime newDateTime = dateTime.minusSeconds(30);
        System.out.println("Date-Time minus 30 seconds: " + newDateTime);
    }
}

Output:

Date-Time minus 30 seconds: 2024-07-05T13:58:08.564001

17. isAfter(LocalDateTime other)

Checks if this date-time is after the specified date-time.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime1 = LocalDateTime.now();
        LocalDateTime dateTime2 = dateTime1.minusHours(1);
        boolean isAfter = dateTime1.isAfter(dateTime2);
        System.out.println("Is after: " + isAfter);
    }
}

Output:

Is after: true

18. isBefore(LocalDateTime other)

Checks if this date-time is before the specified date-time.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime1 = LocalDateTime.now();
        LocalDateTime dateTime2 = dateTime1.plusHours(1);
        boolean isBefore = dateTime1.isBefore(dateTime2);
        System.out.println("Is before: " + isBefore);
    }
}

Output:

Is before: true

19. isEqual(LocalDateTime other)

Checks if this date-time is equal to the specified date-time.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime1 = LocalDateTime.now();
        LocalDateTime dateTime2 = LocalDateTime.from(dateTime1);
        boolean isEqual = dateTime1.isEqual(dateTime2);
        System.out.println("Is equal: " + isEqual);
    }
}

Output:

Is equal: true

20. toString()

Outputs this date-time as a String, such as 2007-12-03T10:15:30.

Example:

import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        System.out.println("Date-Time: " + dateTime.toString());
    }
}

Output:

Date-Time: 2024-07-05T13:58:38.945236

21. toLocalDate()

Gets the LocalDate part of this date-time.

Example:

import java.time.LocalDate;
import java.time.LocalDateTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        LocalDate date = dateTime.toLocalDate();
        System.out.println("Local Date: " + date);
    }
}

Output:

Local Date: 2024-07-05

22. toLocalTime()

Gets the LocalTime part of this date-time.

Example:

import java.time.LocalDateTime;
import java.time.LocalTime;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        LocalDateTime dateTime = LocalDateTime.now();
        LocalTime time = dateTime.toLocalTime();
        System.out.println("Local Time: " + time);
    }
}

Output:

Local Time: 13:58:39.127136

This guide covers the essential methods provided by the LocalDateTime class in Java 8, demonstrating how to create, manipulate, and query date-time objects. Each method is illustrated with working examples to help you understand how to use them effectively in your applications.

My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare