Java LocalDateTime by Examples

Tags: Java LocalDateTime Java 8

The java.time.LocalDateTime class was introduced from Java 8 to represent a date time without a time-zone in the ISO-8601 calendar system, such as 2022-04-20T10:15:30. In this Java core tutorial we learn how to use the java.time.LocalDateTime class in Java programming language with different example Java programs.

Table of contents

  1. Get Current System Date Time using LocalDateTime.now()
  2. Create LocalDateTime object from Year Month Day Hour Minute Second and nano of Second
  3. Create LocalDateTime object from LocalDate and LocalTime
  4. Convert Instant to LocalDateTime
  5. Convert Epoch of Second to LocalDateTime
  6. Convert String to LocalDateTime
  7. Convert LocalDateTime to String
  8. Convert LocalDateTime to LocalDate
  9. Convert LocalDateTime to LocalTime
  10. Convert LocalDateTime to OffsetDateTime
  11. Covnert LocalDateTime to ZonedDateTime
  12. Get Minimum and Maximum of LocalDateTime
  13. Get LocalDateTime Field Values
  14. Set Year to LocalDateTime using withYear() method
  15. Set Month to LocalDateTime using withMonth() method
  16. Set Day of Month to LocalDateTime using withDayOfMonth() method
  17. Set Day of Year to LocalDateTime using withDayOfYear() method
  18. Set Hour to LocalDateTime using withHour() method
  19. Set Minute to LocalDateTime using withMinute() method
  20. Set Second to LocalDateTime using withSecond() method
  21. Set Nano to LocalDateTime using withNano() method
  22. Add Years to LocalDateTime using plusYears() method
  23. Add Months to LocalDateTime using plusMonths() method
  24. Add Weeks to LocalDateTime using plusWeeks() method
  25. Add Days to LocalDateTime using plusDays() method
  26. Add Hours to LocalDateTime using plusHours() method
  27. Add Minutes to LocalDateTime using plusMinutes() method
  28. Add Seconds to LocalDateTime using plusSeconds() method
  29. Add Nano to LocalDateTime using plusNanos() method
  30. Subtract Years from LocalDateTime using minusYears() method
  31. Subtract Months from LocalDateTime using minusMonths() method
  32. Subtract Weeks from LocalDateTime using minusWeeks() method
  33. Subtract Days from LocalDateTime using minusDays() method
  34. Subtract Hours from LocalDateTime using minusHours() method
  35. Subtract Minutes from LocalDateTime using minusMinutes() method
  36. Subtract Seconds from LocalDateTime using minusSeconds() method
  37. Subtract Nanos from LocalDateTime using minusNanos() method
  38. Compare two LocalDateTime objects for Ordering
  39. Compare two LocalDateTime objects for Equality
  40. Check if a LocalDateTime object is before another LocalDateTime object
  41. Check if a LocalDateTime object is after another LocalDateTime object

Get Current System Date Time using LocalDateTime.now()

In Java we can use the LocalDateTime.now() static method to create new object of LocalDateTime which represents the current system date and time from the system lock and default time zone. We can learn how to use the LocalDateTime.now() in Java code below.

LocalDateTimeExample1.java

import java.time.LocalDateTime;

public class LocalDateTimeExample1 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        System.out.println(localDateTime);
    }
}
The output as below.
2022-04-20T20:30:32.758448200

Using the LocalDateTime.now() method we also can create current date time at a specified time zone. For example in the following Java program we get current date time at time zone Europe/Paris.

LocalDateTimeExample2.java

import java.time.LocalDateTime;
import java.time.ZoneId;

public class LocalDateTimeExample2 {
    public static void main(String... args) {
        ZoneId zoneId = ZoneId.of("Europe/Paris");
        LocalDateTime localDateTime = LocalDateTime.now(zoneId);

        System.out.println(localDateTime);
    }
}
The output as below.
2022-04-20T15:30:56.849196100

Or get current date time from a specified clock. For example, we get current UTC date and time as below Java code.

LocalDateTimeExample3.java

import java.time.Clock;
import java.time.LocalDateTime;

public class LocalDateTimeExample3 {
    public static void main(String... args) {
        Clock utcClock = Clock.systemUTC();
        LocalDateTime localDateTime = LocalDateTime.now(utcClock);

        System.out.println(localDateTime);
    }
}
The output as below.
2022-04-20T13:31:23.134655200

Create LocalDateTime object from Year Month Day Hour Minute Second and nano of Second

In Java we can use the LocalDateTime.of() method to create LocalDateTime object with given year, month, day, hour, minute, second or nano of second as the Java code below.

LocalDateTimeExample4.java

import java.time.LocalDateTime;
import java.time.Month;

public class LocalDateTimeExample4 {
    public static void main(String... args) {
        int year = 2022;
        Month month = Month.JULY;
        int monthValue = 7;
        int dayOfMonth = 11;
        int hour = 8;
        int minute = 30;
        int second = 40;
        int nanoOfSecond = 999;

        LocalDateTime localDateTime1 = LocalDateTime.of(year, month, dayOfMonth, hour, minute);
        LocalDateTime localDateTime2 = LocalDateTime.of(year, month, dayOfMonth, hour, minute, second);
        LocalDateTime localDateTime3 = LocalDateTime.of(year, month, dayOfMonth, hour, minute, second, nanoOfSecond);
        LocalDateTime localDateTime4 = LocalDateTime.of(year, monthValue, dayOfMonth, hour, minute);
        LocalDateTime localDateTime5 = LocalDateTime.of(year, monthValue, dayOfMonth, hour, minute, second);
        LocalDateTime localDateTime6 = LocalDateTime.of(year, monthValue, dayOfMonth, hour, minute, second, nanoOfSecond);

        System.out.println(localDateTime1);
        System.out.println(localDateTime2);
        System.out.println(localDateTime3);
        System.out.println(localDateTime4);
        System.out.println(localDateTime5);
        System.out.println(localDateTime6);
    }
}
The output as below.
2022-07-11T08:30
2022-07-11T08:30:40
2022-07-11T08:30:40.000000999
2022-07-11T08:30
2022-07-11T08:30:40
2022-07-11T08:30:40.000000999

Create LocalDateTime object from LocalDate and LocalTime

We also can use the LocalDateTime.of() method to combine the LocalDate object and LocalTime object to a LocalDateTime object as following example Java program.

LocalDateTimeExample5.java

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

public class LocalDateTimeExample5 {
    public static void main(String... args) {
        LocalDate today = LocalDate.now();
        LocalTime now = LocalTime.now();

        LocalDateTime localDateTime = LocalDateTime.of(today, now);

        System.out.println("LocalDate: " + today);
        System.out.println("LocalTime: " + now);
        System.out.println("LocalDateTime: " + localDateTime);
    }
}
The output as below.
LocalDate: 2022-04-20
LocalTime: 20:39:49.295398300
LocalDateTime: 2022-04-20T20:39:49.295398300

Convert Instant to LocalDateTime

In the following example Java program we show how to use the LocalDateTime.ofInstant() method to convert an Instant object into a LocalDateTime object.

LocalDateTimeExample6.java

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

public class LocalDateTimeExample6 {
    public static void main(String... args) {
        Instant instant = Instant.now();
        ZoneId zoneId = ZoneId.systemDefault();

        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);

        System.out.println("Instant: " + instant);
        System.out.println("ZoneId: " + zoneId);
        System.out.println("LocalDateTime: " + localDateTime);
    }
}
The output as below.
Instant: 2022-04-20T13:42:05.453219400Z
ZoneId: Asia/Bangkok
LocalDateTime: 2022-04-20T20:42:05.453219400

Convert Epoch of Second to LocalDateTime

Epoch seconds is the number of seconds from the epoch of 1970-01-01T00:00:00Z . Using the LocalDateTime.ofEpochSecond() method we can convert epoch of second to a LocalDateTime object as below.

LocalDateTimeExample7.java

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;

public class LocalDateTimeExample7 {
    public static void main(String... args) {
        long epochSecond = 999888777;
        int nanoOfSecond = 555555555;
        ZoneOffset zoneOffset = ZoneId.systemDefault().getRules().getOffset(Instant.now());

        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(epochSecond, nanoOfSecond, zoneOffset);

        System.out.println("Number of epoch seconds: " + epochSecond);
        System.out.println("Nano of Second: " + nanoOfSecond);
        System.out.println("ZoneOffset: " + zoneOffset);
        System.out.println("LocalDateTime: " + localDateTime);
    }
}
The output as below.
Number of epoch seconds: 999888777
Nano of Second: 555555555
ZoneOffset: +07:00
LocalDateTime: 2001-09-08T01:52:57.555555555

Convert String to LocalDateTime

In the following Java program we use the LocalDateTime.parse() method to parse a date time String in ISO-8601 format such as 2011-12-03T10:15:30 to a LocalDateTime object.

LocalDateTimeExample8.java

import java.time.LocalDateTime;

public class LocalDateTimeExample8 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.parse("2022-07-11T09:15:30");

        System.out.println(localDateTime);
    }
}
The output as below.
2022-07-11T09:15:30

We also can use LocalDateTime.parse() method with specified custom date and time format defined by DateTimeFormatter class. For example, in the following Java program we convert a date time String in format dd-MM-yyyy HH:mm:ss to LocalDateTime object.

LocalDateTimeExample9.java

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

public class LocalDateTimeExample9 {
    public static void main(String... args) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse("11-07-2022 20:15:30", dateTimeFormatter);

        System.out.println(localDateTime);
    }
}
The output as below.
2022-07-11T20:15:30

Convert LocalDateTime to String

With a given LocalDateTime object we can use the LocalDateTime.format() method to format it as a String with custom date time format define by DateTimeFormatter class. For example in the following Java program we convert a LocalDateTime object to String in format dd-MM-yyyy HH:mm:ss

LocalDateTimeExample10.java

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

public class LocalDateTimeExample10 {
    public static void main(String... args) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.now();
        String localDateTimeString = localDateTime.format(dateTimeFormatter);

        System.out.println(localDateTimeString);
    }
}
The output as below.
20-04-2022 21:24:03

Or we can simple use the to LocalDateTime.toString() method to convert LocalDateTime object to String as below.

LocalDateTimeExample11.java

import java.time.LocalDateTime;

public class LocalDateTimeExample11 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        String localDateTimeString = localDateTime.toString();

        System.out.println(localDateTimeString);
    }
}
The output as below.
2022-04-20T21:24:24.614834200

Convert LocalDateTime to LocalDate

To convert a LocalDateTime object to LocalDate we can use the LocalDateTime.toLocalDate() method as below.

LocalDateTimeExample12.java

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

public class LocalDateTimeExample12 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDate localDate = localDateTime.toLocalDate();

        System.out.println("LocalDateTime: " + localDateTime);
        System.out.println("LocalDate: " + localDate);
    }
}
The output as below.
LocalDateTime: 2022-04-20T21:26:48.085269600
LocalDate: 2022-04-20

Convert LocalDateTime to LocalTime

To convert a LocalDateTime object to LocalTime we can use the LocalDateTime.toLocalTime() method as below.

LocalDateTimeExample13.java

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

public class LocalDateTimeExample13 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalTime localTime = localDateTime.toLocalTime();

        System.out.println("LocalDateTime: " + localDateTime);
        System.out.println("LocalTime: " + localTime);
    }
}
The output as below.
LocalDateTime: 2022-04-20T21:28:13.216820800
LocalTime: 21:28:13.216820800

Convert LocalDateTime to OffsetDateTime

In the following Java program we show how to use the LocalDateTime.atOffset() method in Java to convert a LocalDateTime object to an OffsetDateTime object.

LocalDateTimeExample14.java

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;

public class LocalDateTimeExample14 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        ZoneOffset zoneOffset = ZoneId.systemDefault().getRules().getOffset(Instant.now());
        OffsetDateTime offsetDateTime = localDateTime.atOffset(zoneOffset);

        System.out.println("LocalDateTime: " + localDateTime);
        System.out.println("OffsetDateTime: " + offsetDateTime);
    }
}
The output as below.
LocalDateTime: 2022-04-20T21:34:00.799799300
OffsetDateTime: 2022-04-20T21:34:00.799799300+07:00

Covnert LocalDateTime to ZonedDateTime

In the following Java program we show how to use the LocalDateTime.atZone() method in Java to convert a LocalDateTime object to a ZonedDateTime object.

LocalDateTimeExample15.java

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class LocalDateTimeExample15 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);

        System.out.println("LocalDateTime: " + localDateTime);
        System.out.println("ZonedDateTime: " + zonedDateTime);
    }
}
The output as below.
LocalDateTime: 2022-04-20T21:40:48.053868200
ZonedDateTime: 2022-04-20T21:40:48.053868200+07:00[Asia/Bangkok]

Get Minimum and Maximum of LocalDateTime

The LocalDateTime class provides constants which allow to get minimum or maximum value of LocalDateTime. We learn how to use these constants in the Java code below.

LocalDateTimeExample16.java

import java.time.LocalDateTime;

public class LocalDateTimeExample16 {
    public static void main(String... args) {
        LocalDateTime min = LocalDateTime.MIN;
        LocalDateTime max = LocalDateTime.MAX;

        System.out.println("Minimum LocalDateTime: " + min);
        System.out.println("Maximum LocalDateTime: " + max);
    }
}
The output as below.
Minimum LocalDateTime: -999999999-01-01T00:00
Maximum LocalDateTime: +999999999-12-31T23:59:59.999999999

Get LocalDateTime Field Values

In the following example Java program we show how to use get methods provide by the LocalDateTime class to get fields values of a LocalDateTime object.

LocalDateTimeExample17.java

import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.Month;

public class LocalDateTimeExample17 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.now();

        int year = localDateTime.getYear();
        int monthValue = localDateTime.getMonthValue();
        Month month = localDateTime.getMonth();
        DayOfWeek dayOfWeek = localDateTime.getDayOfWeek();
        int dayOfMonth = localDateTime.getDayOfMonth();
        int dayOfYear = localDateTime.getDayOfYear();
        int hour = localDateTime.getHour();
        int minute = localDateTime.getMinute();
        int second = localDateTime.getSecond();
        int nano = localDateTime.getNano();

        System.out.println("Year: " + year);
        System.out.println("Month: " + monthValue);
        System.out.println("Month: " + month);
        System.out.println("Day of Week: " + dayOfWeek);
        System.out.println("Day of Month: " + dayOfMonth);
        System.out.println("Day of Year: " + dayOfYear);
        System.out.println("Hour: " + hour);
        System.out.println("Minute: " + minute);
        System.out.println("Second: " + second);
        System.out.println("Nano: " + nano);
    }
}
The output as below.
Year: 2022
Month: 4
Month: APRIL
Day of Week: WEDNESDAY
Day of Month: 20
Day of Year: 110
Hour: 21
Minute: 51
Second: 7
Nano: 873441500

Set Year to LocalDateTime using withYear() method

Using the LocalDateTime.withYear() method we can create a new LocalDateTime object with a new year value as the Java code below.

LocalDateTimeExample18.java

import java.time.LocalDateTime;

public class LocalDateTimeExample18 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(1988, 1, 1, 1, 1, 1, 1);

        // set year to 2022
        localDateTime = localDateTime.withYear(2022);

        System.out.println(localDateTime);
    }
}
The output as below.
2022-01-01T01:01:01.000000001

Set Month to LocalDateTime using withMonth() method

Using the LocalDateTime.withMonth() method we can create a new LocalDateTime object with a new month value as the Java code below.

LocalDateTimeExample19.java

import java.time.LocalDateTime;

public class LocalDateTimeExample19 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(1988, 1, 1, 1, 1, 1, 1);

        // set month to 12
        localDateTime = localDateTime.withMonth(12);

        System.out.println(localDateTime);
    }
}
The output as below.
1988-12-01T01:01:01.000000001

Set Day of Month to LocalDateTime using withDayOfMonth() method

Using the LocalDateTime.withDayOfMonth() method we can create a new LocalDateTime object with a new day of month value as the Java code below.

LocalDateTimeExample20.java

import java.time.LocalDateTime;

public class LocalDateTimeExample20 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(1988, 1, 1, 1, 1, 1, 1);

        // set day of month to 25
        localDateTime = localDateTime.withDayOfMonth(25);

        System.out.println(localDateTime);
    }
}
The output as below.
1988-01-25T01:01:01.000000001

Set Day of Year to LocalDateTime using withDayOfYear() method

Using the LocalDateTime.withDayOfYear() method we can create a new LocalDateTime object with a new day of year value as the Java code below.

LocalDateTimeExample21.java

import java.time.LocalDateTime;

public class LocalDateTimeExample21 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(1988, 1, 1, 1, 1, 1, 1);

        // set day of year to 350
        localDateTime = localDateTime.withDayOfYear(350);

        System.out.println(localDateTime);
    }
}
The output as below.
1988-12-15T01:01:01.000000001

Set Hour to LocalDateTime using withHour() method

Using the LocalDateTime.withHour() method we can create a new LocalDateTime object with a new hour value as the Java code below.

LocalDateTimeExample22.java

import java.time.LocalDateTime;

public class LocalDateTimeExample22 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(1988, 1, 1, 1, 1, 1, 1);

        // set hour to 12
        localDateTime = localDateTime.withHour(12);

        System.out.println(localDateTime);
    }
}
The output as below.
1988-01-01T12:01:01.000000001

Set Minute to LocalDateTime using withMinute() method

Using the LocalDateTime.withMinute() method we can create a new LocalDateTime object with a new minute value as the Java code below.

LocalDateTimeExample23.java

import java.time.LocalDateTime;

public class LocalDateTimeExample23 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(1988, 1, 1, 1, 1, 1, 1);

        // set minute to 50
        localDateTime = localDateTime.withMinute(50);

        System.out.println(localDateTime);
    }
}
The output as below.
1988-01-01T01:50:01.000000001

Set Second to LocalDateTime using withSecond() method

Using the LocalDateTime.withSecond() method we can create a new LocalDateTime object with a new second value as the Java code below.

LocalDateTimeExample24.java

import java.time.LocalDateTime;

public class LocalDateTimeExample24 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(1988, 1, 1, 1, 1, 1, 1);

        // set second to 30
        localDateTime = localDateTime.withSecond(30);

        System.out.println(localDateTime);
    }
}
The output as below.
1988-01-01T01:01:30.000000001

Set Nano to LocalDateTime using withNano() method

Using the LocalDateTime.withNano() method we can create a new LocalDateTime object with a new nano of second value as the Java code below.

LocalDateTimeExample25.java

import java.time.LocalDateTime;

public class LocalDateTimeExample25 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(1988, 1, 1, 1, 1, 1, 1);

        // set nano of second to 999
        localDateTime = localDateTime.withNano(999);

        System.out.println(localDateTime);
    }
}
The output as below.
1988-01-01T01:01:01.000000999

Add Years to LocalDateTime using plusYears() method

Using the LocalDateTime.plusYears() method we can create a new LocalDateTime object with number of years be added as Java code below.

LocalDateTimeExample26.java

import java.time.LocalDateTime;

public class LocalDateTimeExample26 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(1988, 1, 1, 1, 1, 1, 1);

        // add 20 years
        localDateTime = localDateTime.plusYears(20);

        System.out.println(localDateTime);
    }
}
The output as below.
2008-01-01T01:01:01.000000001

Add Months to LocalDateTime using plusMonths() method

Using the LocalDateTime.plusMonths() method we can create a new LocalDateTime object with number of months be added as Java code below.

LocalDateTimeExample27.java

import java.time.LocalDateTime;

public class LocalDateTimeExample27 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(1988, 1, 1, 1, 1, 1, 1);

        // add 6 months
        localDateTime = localDateTime.plusMonths(6);

        System.out.println(localDateTime);
    }
}
The output as below.
1988-07-01T01:01:01.000000001

Add Weeks to LocalDateTime using plusWeeks() method

Using the LocalDateTime.plusWeeks() method we can create a new LocalDateTime object with number of weeks be added as Java code below.

LocalDateTimeExample28.java

import java.time.LocalDateTime;

public class LocalDateTimeExample28 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(1988, 1, 1, 1, 1, 1, 1);

        // add 4 weeks
        localDateTime = localDateTime.plusWeeks(4);

        System.out.println(localDateTime);
    }
}
The output as below.
1988-01-29T01:01:01.000000001

Add Days to LocalDateTime using plusDays() method

Using the LocalDateTime.plusDays() method we can create a new LocalDateTime object with number of days be added as Java code below.

LocalDateTimeExample29.java

import java.time.LocalDateTime;

public class LocalDateTimeExample29 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(1988, 1, 1, 1, 1, 1, 1);

        // add 60 days
        localDateTime = localDateTime.plusDays(60);

        System.out.println(localDateTime);
    }
}
The output as below.
1988-03-01T01:01:01.000000001

Add Hours to LocalDateTime using plusHours() method

Using the LocalDateTime.plusHours() method we can create a new LocalDateTime object with number of hours be added as Java code below.

LocalDateTimeExample30.java

import java.time.LocalDateTime;

public class LocalDateTimeExample30 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(1988, 1, 1, 1, 1, 1, 1);

        // add 48 hours
        localDateTime = localDateTime.plusHours(48);

        System.out.println(localDateTime);
    }
}
The output as below.
1988-01-03T01:01:01.000000001

Add Minutes to LocalDateTime using plusMinutes() method

Using the LocalDateTime.plusMinutes() method we can create a new LocalDateTime object with number of minutes be added as Java code below.

LocalDateTimeExample31.java

import java.time.LocalDateTime;

public class LocalDateTimeExample31 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(1988, 1, 1, 1, 1, 1, 1);

        // add 120 minutes
        localDateTime = localDateTime.plusMinutes(120);

        System.out.println(localDateTime);
    }
}
The output as below.
1988-01-01T03:01:01.000000001

Add Seconds to LocalDateTime using plusSeconds() method

Using the LocalDateTime.plusSeconds() method we can create a new LocalDateTime object with number of seconds be added as Java code below.

LocalDateTimeExample32.java

import java.time.LocalDateTime;

public class LocalDateTimeExample32 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(1988, 1, 1, 1, 1, 1, 1);

        // add 180 seconds
        localDateTime = localDateTime.plusSeconds(180);

        System.out.println(localDateTime);
    }
}
The output as below.
1988-01-01T01:04:01.000000001

Add Nano to LocalDateTime using plusNanos() method

Using the LocalDateTime.plusNanos() method we can create a new LocalDateTime object with number of nanos be added as Java code below.

LocalDateTimeExample33.java

import java.time.LocalDateTime;

public class LocalDateTimeExample33 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(1988, 1, 1, 1, 1, 1, 1);

        // add 888888 nano of second
        localDateTime = localDateTime.plusNanos(888888);

        System.out.println(localDateTime);
    }
}
The output as below.
1988-01-01T01:01:01.000888889

Subtract Years from LocalDateTime using minusYears() method

Using the LocalDateTime.minusYears() method we can create a new LocalDateTime object with number of years be subtracted as Java code below.

LocalDateTimeExample34.java

import java.time.LocalDateTime;

public class LocalDateTimeExample34 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);

        // subtract 9 years
        localDateTime = localDateTime.minusYears(9);

        System.out.println(localDateTime);
    }
}
The output as below.
2013-01-01T01:01:01.000000001

Subtract Months from LocalDateTime using minusMonths() method

Using the LocalDateTime.minusMonths() method we can create a new LocalDateTime object with number of months be subtracted as Java code below.

LocalDateTimeExample35.java

import java.time.LocalDateTime;

public class LocalDateTimeExample35 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);

        // subtract 8 months
        localDateTime = localDateTime.minusMonths(8);

        System.out.println(localDateTime);
    }
}
The output as below.
2021-05-01T01:01:01.000000001

Subtract Weeks from LocalDateTime using minusWeeks() method

Using the LocalDateTime.minusWeeks() method we can create a new LocalDateTime object with number of weeks be subtracted as Java code below.

LocalDateTimeExample36.java

import java.time.LocalDateTime;

public class LocalDateTimeExample36 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);

        // subtract 1 week
        localDateTime = localDateTime.minusWeeks(1);

        System.out.println(localDateTime);
    }
}
The output as below.
2021-12-25T01:01:01.000000001

Subtract Days from LocalDateTime using minusDays() method

Using the LocalDateTime.minusDays() method we can create a new LocalDateTime object with number of days be subtracted as Java code below.

LocalDateTimeExample37.java

import java.time.LocalDateTime;

public class LocalDateTimeExample37 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);

        // subtract 20 days
        localDateTime = localDateTime.minusDays(20);

        System.out.println(localDateTime);
    }
}
The output as below.
2021-12-12T01:01:01.000000001

Subtract Hours from LocalDateTime using minusHours() method

Using the LocalDateTime.minusHours() method we can create a new LocalDateTime object with number of hours be subtracted as Java code below.

LocalDateTimeExample38.java

import java.time.LocalDateTime;

public class LocalDateTimeExample38 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);

        // subtract 48 hours
        localDateTime = localDateTime.minusHours(48);

        System.out.println(localDateTime);
    }
}
The output as below.
2021-12-30T01:01:01.000000001

Subtract Minutes from LocalDateTime using minusMinutes() method

Using the LocalDateTime.minusMinutes() method we can create a new LocalDateTime object with number of minutes be subtracted as Java code below.

LocalDateTimeExample39.java

import java.time.LocalDateTime;

public class LocalDateTimeExample39 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);

        // subtract 120 minutes
        localDateTime = localDateTime.minusMinutes(120);

        System.out.println(localDateTime);
    }
}
The output as below.
2021-12-31T23:01:01.000000001

Subtract Seconds from LocalDateTime using minusSeconds() method

Using the LocalDateTime.minusSeconds() method we can create a new LocalDateTime object with number of seconds be subtracted as Java code below.

LocalDateTimeExample40.java

import java.time.LocalDateTime;

public class LocalDateTimeExample40 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);

        // subtract 600 seconds
        localDateTime = localDateTime.minusSeconds(600);

        System.out.println(localDateTime);
    }
}
The output as below.
2022-01-01T00:51:01.000000001

Subtract Nanos from LocalDateTime using minusNanos() method

Using the LocalDateTime.minusNanos() method we can create a new LocalDateTime object with number of nanos be subtracted as Java code below.

LocalDateTimeExample41.java

import java.time.LocalDateTime;

public class LocalDateTimeExample41 {
    public static void main(String... args) {
        LocalDateTime localDateTime = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);

        // subtract 777777 nano of second
        localDateTime = localDateTime.minusNanos(777777);

        System.out.println(localDateTime);
    }
}
The output as below.
2022-01-01T01:01:00.999222224

Compare two LocalDateTime objects for Ordering

In Java the LocalDateTime.compareTo() method to return value of -1, 0, 1 if a LocalDateTime object is before, equal to or after another specified LocalDateTime object it compare to. We learn how to use the LocalDateTime.compareTo() method in the following example Java program.

LocalDateTimeExample42.java

import java.time.LocalDateTime;

public class LocalDateTimeExample42 {
    public static void main(String... args) {
        LocalDateTime localDateTime1 = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);
        LocalDateTime localDateTime2 = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);
        LocalDateTime localDateTime3 = LocalDateTime.of(2022, 2, 2, 2, 2, 2, 2);

        int compareToResult1 = localDateTime1.compareTo(localDateTime2);
        int compareToResult2 = localDateTime1.compareTo(localDateTime3);
        int compareToResult3 = localDateTime3.compareTo(localDateTime1);

        System.out.println("localDateTime1: " + localDateTime1);
        System.out.println("localDateTime2: " + localDateTime2);
        System.out.println("localDateTime3: " + localDateTime3);
        System.out.println("localDateTime1 compareTo localDateTime2: " + compareToResult1);
        System.out.println("localDateTime1 compareTo localDateTime3: " + compareToResult2);
        System.out.println("localDateTime3 compareTo localDateTime1: " + compareToResult3);
    }
}
The output as below.
localDateTime1: 2022-01-01T01:01:01.000000001
localDateTime2: 2022-01-01T01:01:01.000000001
localDateTime3: 2022-02-02T02:02:02.000000002
localDateTime1 compareTo localDateTime2: 0
localDateTime1 compareTo localDateTime3: -1
localDateTime3 compareTo localDateTime1: 1

Compare two LocalDateTime objects for Equality

To compare two LocalDateTime objects for equality in Java we can use the LocalDateTime.isEqual() method as the Java code below.

LocalDateTimeExample43.java

import java.time.LocalDateTime;

public class LocalDateTimeExample43 {
    public static void main(String... args) {
        LocalDateTime localDateTime1 = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);
        LocalDateTime localDateTime2 = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);
        LocalDateTime localDateTime3 = LocalDateTime.of(2022, 2, 2, 2, 2, 2, 2);

        boolean isEqualResult1 = localDateTime1.isEqual(localDateTime2);
        boolean isEqualResult2 = localDateTime1.isEqual(localDateTime3);

        System.out.println("localDateTime1: " + localDateTime1);
        System.out.println("localDateTime2: " + localDateTime2);
        System.out.println("localDateTime3: " + localDateTime3);
        System.out.println("localDateTime1 is equal to localDateTime2: " + isEqualResult1);
        System.out.println("localDateTime1 is equal to localDateTime3: " + isEqualResult2);
    }
}
The output as below.
localDateTime1: 2022-01-01T01:01:01.000000001
localDateTime2: 2022-01-01T01:01:01.000000001
localDateTime3: 2022-02-02T02:02:02.000000002
localDateTime1 is equal to localDateTime2: true
localDateTime1 is equal to localDateTime3: false

Or we can use the LocalDateTime.equals() method which return the same result of comparison.

LocalDateTimeExample44.java

import java.time.LocalDateTime;

public class LocalDateTimeExample44 {
    public static void main(String... args) {
        LocalDateTime localDateTime1 = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);
        LocalDateTime localDateTime2 = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);
        LocalDateTime localDateTime3 = LocalDateTime.of(2022, 2, 2, 2, 2, 2, 2);

        boolean equalsResult1 = localDateTime1.equals(localDateTime2);
        boolean equalsResult2 = localDateTime1.equals(localDateTime3);

        System.out.println("localDateTime1: " + localDateTime1);
        System.out.println("localDateTime2: " + localDateTime2);
        System.out.println("localDateTime3: " + localDateTime3);
        System.out.println("localDateTime1 is equal to localDateTime2: " + equalsResult1);
        System.out.println("localDateTime1 is equal to localDateTime3: " + equalsResult2);
    }
}
The output as below.
localDateTime1: 2022-01-01T01:01:01.000000001
localDateTime2: 2022-01-01T01:01:01.000000001
localDateTime3: 2022-02-02T02:02:02.000000002
localDateTime1 is equal to localDateTime2: true
localDateTime1 is equal to localDateTime3: false

Check if a LocalDateTime object is before another LocalDateTime object

The LocalDateTime class provides the isBefore() method which can be used to check if a LocalDateTime object is before another LocalDateTime object or not. We learn how to use the LocalDateTime.isBefore() method in the following example Java code.

LocalDateTimeExample45.java

import java.time.LocalDateTime;

public class LocalDateTimeExample45 {
    public static void main(String... args) {
        LocalDateTime localDateTime1 = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);
        LocalDateTime localDateTime2 = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);
        LocalDateTime localDateTime3 = LocalDateTime.of(2022, 2, 2, 2, 2, 2, 2);

        boolean equalsResult1 = localDateTime1.isBefore(localDateTime2);
        boolean equalsResult2 = localDateTime1.isBefore(localDateTime3);

        System.out.println("localDateTime1: " + localDateTime1);
        System.out.println("localDateTime2: " + localDateTime2);
        System.out.println("localDateTime3: " + localDateTime3);
        System.out.println("localDateTime1 is before localDateTime2: " + equalsResult1);
        System.out.println("localDateTime1 is before localDateTime3: " + equalsResult2);
    }
}
The output as below.
localDateTime1: 2022-01-01T01:01:01.000000001
localDateTime2: 2022-01-01T01:01:01.000000001
localDateTime3: 2022-02-02T02:02:02.000000002
localDateTime1 is before localDateTime2: false
localDateTime1 is before localDateTime3: true

Check if a LocalDateTime object is after another LocalDateTime object

The LocalDateTime class provides the isAfter() method which can be used to check if a LocalDateTime object is after another LocalDateTime object or not. We learn how to use the LocalDateTime.isAfter() method in the following example Java code.

LocalDateTimeExample46.java

import java.time.LocalDateTime;

public class LocalDateTimeExample46 {
    public static void main(String... args) {
        LocalDateTime localDateTime1 = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);
        LocalDateTime localDateTime2 = LocalDateTime.of(2022, 1, 1, 1, 1, 1, 1);
        LocalDateTime localDateTime3 = LocalDateTime.of(1999, 1, 1, 1, 1, 1, 1);

        boolean equalsResult1 = localDateTime1.isAfter(localDateTime2);
        boolean equalsResult2 = localDateTime1.isAfter(localDateTime3);

        System.out.println("localDateTime1: " + localDateTime1);
        System.out.println("localDateTime2: " + localDateTime2);
        System.out.println("localDateTime3: " + localDateTime3);
        System.out.println("localDateTime1 is before localDateTime2: " + equalsResult1);
        System.out.println("localDateTime1 is before localDateTime3: " + equalsResult2);
    }
}
The output as below.
localDateTime1: 2022-01-01T01:01:01.000000001
localDateTime2: 2022-01-01T01:01:01.000000001
localDateTime3: 1999-01-01T01:01:01.000000001
localDateTime1 is before localDateTime2: false
localDateTime1 is before localDateTime3: true

Happy Coding 😊

Java LocalTime by Examples

Java LocalDate by Examples

Java Date by Examples

Java Calendar by Examples

Java Calendar using Calendar.Builder by Examples