Learnitweb

Java LocalDateTime class with examples

1. Brief Description

  • LocalDateTime class is available in java.time package. java.time package was added in Java 8.
  • This class represents a date and time without a time-zone in the ISO-8601 calendar system.
  • The date-time is usually represented as year-month-day-hour-minute-second, such as 2021-12-03T15:30:40. Time is represented to nanosecond precision.
  • This class does not store or represent time-zone.
  • This class is immutable, thread-safe and value based.

2. How to create instance of LocalDateTime

We’ll discuss few of the common ways of creating a LocalDateTime instance.

2.1 LocalDateTime representing current date and time

LocalDateTime now = LocalDateTime.now();
import java.time.LocalDateTime;

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

Output

Current time : 2021-03-25T23:28:30.367

2.2 LocalDateTime instance using method ‘of’

Following are commonly used overloaded versions of method of to get instance of LocalDateTime. LocalDateTime represents time in nano second precision. Default value is set to 0 if you don’t provide value for second or nano second.

2.2.1 static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute)

LocalDateTime localDateTime = LocalDateTime.of(1, 5, 15, 10, 30);  
>> 0001-05-15T10:30

2.2.2 static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)

LocalDateTime localDateTime = LocalDateTime.of(1, 5, 15, 10, 30, 45);
>> 0001-05-15T10:30:45

2.2.3 static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)

LocalDateTime localDateTime = LocalDateTime.of(1, 5, 15, 10, 30, 45, 20); 
>> 0001-05-15T10:30:45.000000020

2.2.4 static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute)

LocalDateTime localDateTime = LocalDateTime.of(1, Month.JULY, 15, 10, 30);
>> 0001-07-15T10:30

2.2.5 static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second)

LocalDateTime localDateTime = LocalDateTime.of(1, Month.JULY, 15, 10, 30, 25);
>> 0001-07-15T10:30:25

2.2.6 static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)

LocalDateTime localDateTime = LocalDateTime.of(1, Month.JULY, 15, 10, 30, 25, 17);
>> 0001-07-15T10:30:25.000000017

2.2.7 static LocalDateTime of(LocalDate date, LocalTime time)

LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime); 
>> 2021-03-25T23:56:10.745

2.3 LocalDateTime instance from String

We can use parse method and DateTimeFormatter to format the date and time in specific format.

//Default date time pattern
String time = "2021-03-26T10:15:30";
LocalDateTime localTimeObj = LocalDateTime.parse(time);
 
//Specific date time pattern
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a");
String time1 = "2021-03-26 10:15:30 AM";
LocalDateTime localTimeObj1 = LocalDateTime.parse(time1, formatter);

3. LocalDateTime comparison

Following are commonly used methods for comparison:

boolean isAfter(ChronoLocalDateTime other)Checks if this date-time is after the specified date-time
boolean isBefore(ChronoLocalDateTime other)Checks if this date-time is before the specified date-time
boolean isEqual(ChronoLocalDateTime other)Checks if this date-time is equal to the specified date-time
int compareTo(ChronoLocalDateTime other)Compares this date-time to another date-time
import java.time.LocalDateTime;

public class LocalDateTimeExample {
	public static void main(String[] args) {
		LocalDateTime today = LocalDateTime.now();
		LocalDateTime yesterday = LocalDateTime.parse("2021-03-25T10:17:30");
		System.out.println(today.isAfter(yesterday));
		System.out.println(today.isBefore(yesterday));
		System.out.println(today.isEqual(yesterday));
		System.out.println(today.compareTo(yesterday));
	}
}

Output

true
false
false
1

4. Get information from LocalDateTime instance

We can get information like day of month, month, year, hour, minute, second and nano second from the LocalDateTime instance. Following are the commonly used methods:

int getDayOfMonth()Returns the day-of-month field
DayOfWeek getDayOfWeek()Returns the day-of-week field
int getDayOfYear()Returns the day-of-year field
int getHour()Returns the hour-of-day field
int getMinute()Returns the minute-of-hour field
Month getMonth()Returns the month-of-year field using the Month enum
int getMonthValue()Returns the month-of-year field from 1 to 12
int getNano()Returns the nano-of-second field
int getSecond()Returns the second-of-minute field
int getYear()Returns the year field
import java.time.LocalDateTime;

public class LocalDateTimeExample {
	public static void main(String[] args) {
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("Day of month: " + localDateTime.getDayOfMonth());
		System.out.println("Day of week: " + localDateTime.getDayOfWeek());
		System.out.println("Day of year: " + localDateTime.getDayOfYear());
		System.out.println("Hour: " + localDateTime.getHour());
		System.out.println("Minute: " + localDateTime.getMinute());
		System.out.println("Month: " + localDateTime.getMonth());
		System.out.println("Month Value: " + localDateTime.getMonthValue());
		System.out.println("Nano: " + localDateTime.getNano());
		System.out.println("Second: " + localDateTime.getSecond());
		System.out.println("Year: " + localDateTime.getYear());
	}
}

Output

Day of month: 26
Day of week: FRIDAY
Day of year: 85
Hour: 0
Minute: 35
Month: MARCH
Month Value: 3
Nano: 316000000
Second: 50
Year: 2021

5. LocalDateTime manipulation

Following are commonly used methods to manipulate LocalDateTime.

LocalDateTime minus(long amountToSubtract, TemporalUnit unit)Minus specified amount of time unit
LocalDateTime minusDays(long days)Minus specified number of days
LocalDateTime minusHours(long hours)Minus specified number of hours
LocalDateTime minusMinutes(long minutes)Minus specified number of minutes
LocalDateTime minusMonths(long months)Minus specified number of months
LocalDateTime minusNanos(long nanos)Minus specified number of nano seconds
LocalDateTime minusSeconds(long seconds)Minus specified number of seconds
LocalDateTime minusWeeks(long weeks)Minus specified number of weeks
LocalDateTime minusYears(long years)minus specified number of years
LocalDateTime plus(long amountToAdd, TemporalUnit unit)Plus specified amount of time unit
LocalDateTime plusDays(long days)Plus specified number of days
LocalDateTime plusHours(long hours)Plus specified number of hours
LocalDateTime plusMinutes(long minutes)Plus specified number of minutes
LocalDateTime plusMonths(long months)Plus specified number of months
LocalDateTime plusNanos(long nanos)Plus specified number of nano seconds
LocalDateTime plusSeconds(long seconds)Plus specified number of seconds
LocalDateTime plusWeeks(long weeks)Plus specified number of weeks
LocalDateTime plusYears(long years)Plus specified number of years
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class LocalDateTimeExample {
	public static void main(String[] args) {
		LocalDateTime localDateTime = LocalDateTime.now();
		System.out.println("Current time: " + localDateTime);
		System.out.println("Current time minus 10 days: " + localDateTime.minus(10, ChronoUnit.DAYS));
		System.out.println("Current time minus 10 hours: " + localDateTime.minusHours(10));
		System.out.println("Current time minus 10 minutes: " + localDateTime.minusMinutes(10));
		System.out.println("Current time minus 10 months: " + localDateTime.minusMonths(10));
		System.out.println("Current time minus 10 nanos: " + localDateTime.minusNanos(10));
		System.out.println("Current time minus 10 seconds: " + localDateTime.minusSeconds(10));
		System.out.println("Current time minus 10 weeks: " + localDateTime.minusWeeks(10));
		System.out.println("Current time minus 10 years: " + localDateTime.minusYears(10));
		
		System.out.println("Current time plus 10 days: " + localDateTime.plus(10, ChronoUnit.DAYS));
		System.out.println("Current time plus 10 days: " + localDateTime.plusDays(10));
		System.out.println("Current time plus 10 hours: " + localDateTime.plusHours(10));
		System.out.println("Current time plus 10 minutes: " + localDateTime.plusMinutes(10));
		System.out.println("Current time plus 10 months: " + localDateTime.plusMonths(10));
		System.out.println("Current time plus 10 nanos: " + localDateTime.plusNanos(10));
		System.out.println("Current time plus 10 seconds: " + localDateTime.plusSeconds(10));
		System.out.println("Current time plus 10 weeks: " + localDateTime.plusWeeks(10));
		System.out.println("Current time plus 10 years: " + localDateTime.plusYears(10));
	}
}

Output

Current time: 2021-03-26T01:02:46.860
Current time minus 10 days: 2021-03-16T01:02:46.860
Current time minus 10 hours: 2021-03-25T15:02:46.860
Current time minus 10 minutes: 2021-03-26T00:52:46.860
Current time minus 10 months: 2020-05-26T01:02:46.860
Current time minus 10 nanos: 2021-03-26T01:02:46.859999990
Current time minus 10 seconds: 2021-03-26T01:02:36.860
Current time minus 10 weeks: 2021-01-15T01:02:46.860
Current time minus 10 years: 2011-03-26T01:02:46.860
Current time plus 10 days: 2021-04-05T01:02:46.860
Current time plus 10 days: 2021-04-05T01:02:46.860
Current time plus 10 hours: 2021-03-26T11:02:46.860
Current time plus 10 minutes: 2021-03-26T01:12:46.860
Current time plus 10 months: 2022-01-26T01:02:46.860
Current time plus 10 nanos: 2021-03-26T01:02:46.860000010
Current time plus 10 seconds: 2021-03-26T01:02:56.860
Current time plus 10 weeks: 2021-06-04T01:02:46.860
Current time plus 10 years: 2031-03-26T01:02:46.860