Java LocalDateTime class

java.time.LocalDateTime class, introduced in Java 8, represents a local date time object without timezone information. It represents time to nanosecond precision e.g. 2007-12-03T10:15:30:55.123456789

We can use the LocalDateTime instances where we need to represent the time without any need of the timezone reference. For example, we can use LocalDateTime to trigger the batch jobs in any application. Jobs will be executed at specific time in that timezone where the server resides.

Practically, to make it more useful, we should use LocalDateTime with additional information such as an offset or time-zone.

Note that LocalDateTime instances are immutable and thread-safe.

1. LocalDateTime class declaration

public final class LocalDateTime
	extends Object
	implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable
{
	//class body
}

2. How to create LocalDateTime

Generally, we will be creating LocalDateTime instances in two conditions i.e. get current time or create local date time for a specified timestamp.

2.1. Get current time

Use following factory method to get the current local date time.

LocalDateTime now = LocalDateTime.now();

2.2. Create specified time

To create a local time with specific date and time information – use below methods.

//Milliseconds precision
LocalDateTime localDateTime1 = 
		LocalDateTime.of(2019, 03, 28, 14, 33, 48, 123456789);

//Month enum
LocalDateTime localDateTime2 = 
		LocalDateTime.of(2019, Month.MARCH, 28, 14, 33, 48, 123456789);

//seconds precision
LocalDateTime localDateTime3 = 
		LocalDateTime.of(2019, Month.MARCH, 28, 14, 33, 48);

//minutes precision
LocalDateTime localDateTime4 = 
		LocalDateTime.of(2019, Month.MARCH, 28, 14, 33);

//local date + local time
LocalDate date = LocalDate.of(2109, 03, 28);
LocalTime time = LocalTime.of(10, 34);	

LocalDateTime localDateTime5 = LocalDateTime.of(date, time);	

3. How to parse string to LocalDateTime

The LocalDateTime class has two overloaded parse() methods to convert time in string to local time instance.

parse(CharSequence text)	//1

parse(CharSequence text, DateTimeFormatter formatter)	//2
  • Use first method if the string contains time in ISO_LOCAL_DATE_TIME pattern i.e. 2019-03-27T10:15:30. This is default pattern of local time in Java.
  • For any other date-time pattern, we need to use second method where we pass the time as string as well as a formatter which represents the pattern of that date-time string.
//1 - default time pattern
String time = "2019-03-27T10:15:30";
LocalDateTime localTimeObj = LocalDateTime.parse(time);

//2 - specific date time pattern
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a");
String time1 = "2019-03-27 10:15:30 AM";
LocalDateTime localTimeObj1 = LocalDateTime.parse(time1, formatter);

4. How to format LocalDateTime to string

Use LocalDateTime.format(DateTimeFormatter) method to format a local time to a desired string representation.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss a");

LocalDateTime now = LocalDateTime.now();

String dateTimeString = now.format(formatter);	//2019-03-28 14:47:33 PM

5. How to modify local time

LocalDateTime provides below methods which can be used to get to new localdatetime instance relative to available localdatetime instance.

  • plusYears()
  • plusMonths()
  • plusDays()
  • plusHours()
  • plusMinutes()
  • plusSeconds()
  • plusNanos()
  • minusYears()
  • minusMonths()
  • minusDays()
  • minusHours()
  • minusMinutes()
  • minusSeconds()
  • minusNanos()
LocalDateTime now = LocalDateTime.now();

//3 hours later
LocalDateTime localDateTime1 = now.plusHours(3);	

//3 minutes earliar
LocalDateTime localDateTime2 = now.minusMinutes(3);

//Next year same time
LocalDateTime localDateTime2 = now.plusYears(1);

//Last year same time
LocalDateTime localDateTime2 = now.minusYears(1);

6. LocalDateTime Examples

Compare LocalDateTime instances
Convert between LocalDateTime to Date
Convert between LocalDateTime to ZonedDateTime
Format LocalDateTime to String
Parse string to LocalDateTime
Convert LocalDate to LocalDateTime in Java

Drop me your questions related to Java 8 LocalDateTime class in comments.

Happy Learning !!

Ref : LocalDateTime Java Doc

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.