ZetCode

Java LocalDateTime

last modified January 27, 2024

In this article we show how to work with LocalDateTime in Java.

Java LocalDateTime

LocalDateTime is an immutable date-time object that represents a date-time. It is a date-time without a time-zone in the ISO-8601 calendar system.

LocalDateTime does not store or represent a time-zone. It cannot represent an instant on the time-line without additional information such as an offset or time-zone. Value 2022-10-27T11:01:19.031990446 is an example of a LocalDateTime.

Java LocalDateTime current time

The current time is retrived with LocalDateTime.now.

com/zetcode/LocalDateTimeEx.java
package com.zetcode;

import java.time.LocalDateTime;

public class LocalDateTimeEx {

    public static void main(String[] args) {

        var now = LocalDateTime.now();
        System.out.println(now);
    }
}

The example prints the current local datetime value.

2022-10-27T11:35:12.339662147

Java LocalDateTime create

The are several ways to create LocalDateTime.

com/zetcode/LocalDateTimeEx.java
package com.zetcode;

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

public class LocalDateTimeEx {

    public static void main(String[] args) {

        var now = LocalDateTime.now();
        System.out.println(now);

        LocalDateTime dt1 = LocalDateTime.of(2022, 10, 27, 10, 40, 55);
        System.out.println(dt1);

        LocalDateTime dt2 = LocalDateTime.parse("2020-11-27T11:20:50", 
            DateTimeFormatter.ISO_LOCAL_DATE_TIME);
        System.out.println(dt2);
    }
}

The program creates three datetime objects.

var now = LocalDateTime.now();

The current datetime is created with LocalDateTime.now.

LocalDateTime dt1 = LocalDateTime.of(2022, 10, 27, 10, 40, 55);

Here a localdatetime value is created with LocalDateTime.of. We specify the year, month, day, hour, and second parts.

LocalDateTime dt2 = LocalDateTime.parse("2020-11-27T11:20:50", 
    DateTimeFormatter.ISO_LOCAL_DATE_TIME);

A localdatetime value is parsed from a string.

2022-10-27T11:33:07.248725811
2022-10-27T10:40:55
2020-11-27T11:20:50

Java LocalDateTime parts

In the next example, we get the current LocalDateTime value parts.

com/zetcode/LocalDateTimeEx.java
package com.zetcode;

import java.time.LocalDateTime;

public class LocalDateTimeEx {

    public static void main(String[] args) {

        LocalDateTime dt = LocalDateTime.now();

        System.out.printf("Year: %s%n", dt.getYear());
        System.out.printf("Month: %s%n", dt.getMonth());
        System.out.printf("Day of month: %s%n", dt.getDayOfMonth());
        System.out.printf("Hour: %s%n", dt.getHour());
        System.out.printf("Minute: %s%n", dt.getMinute());
        System.out.printf("Second: %s%n", dt.getSecond());
    }
}

The getYear gets the year part, the getMonth the month part, the getDayOfMonth gets the day of the month. The getHour gets the hour part, the getMinute gets the minute part, and the getSecond the second part.

Year: 2022
Month: OCTOBER
Day of month: 27
Hour: 11
Minute: 44
Second: 13

Java LocalDateTime format

The datetime is formatted differently in various countries. DateTimeFormatter helps us format the datetime.

com/zetcode/LocalDateTimeEx.java
package com.zetcode;

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

public class LocalDateTimeEx {

    public static void main(String[] args) {

        LocalDateTime now = LocalDateTime.now();

        DateTimeFormatter dtf = DateTimeFormatter.ISO_TIME;
        System.out.println(now.format(dtf));

        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("EEEE, MMM dd, yyyy HH:mm:ss a");
        System.out.println(now.format(dtf2));

        DateTimeFormatter dtf3 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
        System.out.println(now.format(dtf3));
    }
}

The example uses DateTimeFormatter to format time.

DateTimeFormatter dtf = DateTimeFormatter.ISO_TIME;
System.out.println(now.format(dtf));

We format the time to the ISO format time standart.

DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("EEEE, MMM dd, yyyy HH:mm:ss a");

We can choose a specific time format with DateTimeFormatter.ofPattern. The documentation to DateTimeFormatter contains the description of various formatting characters that we can use.

DateTimeFormatter dtf3 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);

Here we use the FormatStyle.MEDIUM format.

12:31:48.14020717
Thursday, Oct 27, 2022 12:31:48 PM
Oct 27, 2022, 12:31:48 PM

Java LocalTime arithmetic

Java LocalDateTimeEx has methods for doing datetime arithmetics.

com/zetcode/LocalDateTimeEx.java
package com.zetcode;

import java.time.LocalDateTime;

public class LocalDateTimeEx {

    public static void main(String[] args) {

        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        // LocalDateTime addition
        System.out.println("Adding 4 years: " + now.plusYears(4));
        System.out.println("Adding 11 months: " + now.plusMonths(11));
        System.out.println("Adding 54 days: " + now.plusDays(54));
        System.out.println("Adding 3 hours: " + now.plusHours(3));
        System.out.println("Adding 30 minutes: " + now.plusMinutes(30));
        System.out.println("Adding 45 seconds: " + now.plusSeconds(45));
        System.out.println("Adding 40000 nanoseconds: " + now.plusNanos(40000));

        System.out.println("-------------------------------------");

        // LocalDateTime subtraction
        System.out.println("Subtracting 4 years: " + now.minusYears(4));
        System.out.println("Subtracting 11 months: " + now.minusMonths(11));
        System.out.println("Subtracting 54 days: " + now.minusDays(54));
        System.out.println("Subtracting 3 hours: " + now.minusHours(3));
        System.out.println("Subtracting 30 minutes: " + now.minusMinutes(30));
        System.out.println("Subtracting 45 seconds: " + now.minusSeconds(45));
        System.out.println("Subtracting 40000 nanoseconds: " + now.minusNanos(40000));

    }
}

The example presents method for adding and subtracting datetime units.

System.out.println("Adding 3 hours: " + localTime.plusHours(3));

The plusHours adds three hours to the current local time.

System.out.println("Subtracting 3 hours: " + now.minusHours(3));

Likewise, the minusHours subtracts three hours from the current local time.

2022-10-27T12:12:13.895367393
Adding 4 years: 2026-10-27T12:12:13.895367393
Adding 11 months: 2023-09-27T12:12:13.895367393
Adding 54 days: 2022-12-20T12:12:13.895367393
Adding 3 hours: 2022-10-27T15:12:13.895367393
Adding 30 minutes: 2022-10-27T12:42:13.895367393
Adding 45 seconds: 2022-10-27T12:12:58.895367393
Adding 40000 nanoseconds: 2022-10-27T12:12:13.895407393
-------------------------------------
Subtracting 4 years: 2018-10-27T12:12:13.895367393
Subtracting 11 months: 2021-11-27T12:12:13.895367393
Subtracting 54 days: 2022-09-03T12:12:13.895367393
Subtracting 3 hours: 2022-10-27T09:12:13.895367393
Subtracting 30 minutes: 2022-10-27T11:42:13.895367393
Subtracting 45 seconds: 2022-10-27T12:11:28.895367393
Subtracting 40000 nanoseconds: 2022-10-27T12:12:13.895327393

Java LocalTime until

With the until method, we can compute the time until another time in terms of the specified unit.

com/zetcode/LocalDateTimeEx.java
package com.zetcode;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class LocalDateTimeEx {

    public static void main(String[] args) {

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime xmas = LocalDateTime.of(now.getYear(), 12, 24, 0, 0 ,0);

        System.out.println(now);
        System.out.println(xmas);

        System.out.printf("%s hours%n", now.until(xmas, ChronoUnit.HOURS));
        System.out.printf("%s minutes%n", now.until(xmas, ChronoUnit.MINUTES));
        System.out.printf("%s seconds%n", now.until(xmas, ChronoUnit.SECONDS));
    }
}

The example calculates the time that has to elapse until another time in hours, minutes and seconds.

System.out.printf("%s hours%n", now.until(time, ChronoUnit.HOURS));

With ChronoUnit.HOURS, we specify that we calculate the the time difference in hours.

2022-10-27T12:13:37.464995847
2022-12-24T00:00
1379 hours
82786 minutes
4967182 seconds

Java LocalTime isBefore/isAfter

We can check if a datetime value is before or after another datetime value with isBefore and isAfter.

com/zetcode/LocalDateTimeEx.java
package com.zetcode;

import java.time.LocalDateTime;

public class LocalDateTimeEx {

    public static void main(String[] args) {

        LocalDateTime now = LocalDateTime.now();
        LocalDateTime dt1 = now.plusYears(3);
        LocalDateTime dt2 = now.minusYears(3);

        if (dt1.isAfter(now)) {
            System.out.printf("%s is after %s%n", dt1, now);
        }

        if (dt2.isBefore(now)) {
            System.out.printf("%s is before %s%n", dt2, now);
        }
    }
}

The program uses the isBefore and isAfter methods to check the current datetime value against two other datetimes.

2025-10-27T12:25:45.261416509 is after 2022-10-27T12:25:45.261416509
2019-10-27T12:25:45.261416509 is before 2022-10-27T12:25:45.261416509

Java LocalDateTime truncate

The LocalDateTime's truncatedTo method returns a copy of a local time with the time truncated. It returns a copy of the original date-time with fields smaller than the specified unit set to zero.

com/zetcode/LocalDateTimeEx.java
package com.zetcode;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class LocalDateTimeEx {

    public static void main(String[] args) {

        LocalDateTime now = LocalDateTime.now();

        System.out.println(now);
        System.out.println(now.truncatedTo(ChronoUnit.HALF_DAYS));
        System.out.println(now.truncatedTo(ChronoUnit.HOURS));
        System.out.println(now.truncatedTo(ChronoUnit.MINUTES));
        System.out.println(now.truncatedTo(ChronoUnit.SECONDS));
        System.out.println(now.truncatedTo(ChronoUnit.MICROS));
    }
}

The example uses truncatedTo to truncate time to half days, hours, minutes, seconds, and micros.

2022-10-27T11:49:32.109348052
2022-10-27T00:00
2022-10-27T11:00
2022-10-27T11:49
2022-10-27T11:49:32
2022-10-27T11:49:32.109348

Source

Java LocalDateTime - language reference

In this article we have worked with Java LocalDateTime.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Java tutorials.