java.time.LocalDateTime Class in Java
Last Updated :
26 May, 2021
java.time.LocalDateTime class, introduced in Java 8, represents a local date-time object without timezone information. The LocalDateTime class in Java is an immutable date-time object that represents a date in the yyyy-MM-dd-HH-mm-ss.zzz format. It implements the ChronoLocalDateTime interface and inherits the object class.
Wherever we need to represent time without a timezone reference, we can use the LocalDateTime instances. LocalDateTime, for example, can be used to start batch jobs in any application. Jobs will be run at a fixed time in the timezone in which the server is located. Note LocalDateTime instances are immutable and thread.
Syntax: Class declaration
public final class LocalDateTime
extends Object
implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable
Methods of this class are as follows:
Method |
Description |
format() |
It is used to format this date-time using the specified formatter. |
get() |
It is used to get the value of the specified field from this date-time as an int. |
minusMinutes() |
Returns a copy of this LocalDateTime with the specified number of minutes subtracted. |
minusYears() |
Returns a copy of this LocalDateTime with the specified number of years subtracted. |
minusDays() |
Returns a copy of this LocalDateTime with the specified number of days subtracted. |
now() |
It is used to obtain the current date-time from the system clock in the default time zone. |
plusHours() |
Returns a copy of this LocalDateTime with the specified number of hours added. |
plusYears() |
Returns a copy of this LocalDateTime with the specified number of years added. |
plusDays() |
Returns a copy of this LocalDateTime with the specified number of days added. |
Some more methods to modify local time are as follows in LocalDateTime can be used to get to a new localdatetime instance relative to an existing localdatetime instance. They are namely as follows:
plusYears(), plusMonths(), plusDays(), plusHours(), plusMinutes(), plusSeconds(), plusNanos(), minusYears(), minusMonths(), minusDays(), minusHours(), minusMinutes(), minusSeconds(), minusNanos()
Examples 1:
Java
import java.time.LocalDateTime;
public class GFG {
public static void main(String[] args)
{
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
LocalDateTime localDateTime1 = now.plusYears( 1 )
.plusMonths( 1 )
.plusWeeks( 1 )
.plusDays( 1 );
System.out.println(localDateTime1);
LocalDateTime localDateTime2
= localDateTime1.minusYears( 1 )
.minusMonths( 1 )
.minusWeeks( 1 )
.minusDays( 1 );
System.out.println(localDateTime2);
LocalDateTime localDateTime3
= localDateTime2.plusHours( 1 )
.plusMinutes( 1 )
.plusSeconds( 1 )
.plusNanos( 100 );
System.out.println(localDateTime3);
LocalDateTime localDateTime4
= localDateTime3.minusHours( 1 )
.minusMinutes( 1 )
.minusSeconds( 1 )
.minusNanos( 100 );
System.out.println(localDateTime4);
}
}
|
Output:
Example 2: Creating a specified time
Java
import java.time.*;
import java.time.format.*;
class GFG {
public static void main(String[] args)
{
LocalDateTime localDateTime1 = LocalDateTime.of(
2021 , 04 , 24 , 14 , 33 , 48 , 123456789 );
System.out.println(localDateTime1);
LocalDateTime localDateTime2 = LocalDateTime.of(
2021 , Month.APRIL, 24 , 14 , 33 , 48 , 123456789 );
System.out.println(localDateTime2);
LocalDateTime localDateTime3 = LocalDateTime.of(
2021 , Month.APRIL, 24 , 14 , 33 , 48 );
System.out.println(localDateTime3);
LocalDateTime localDateTime4 = LocalDateTime.of(
2021 , Month.APRIL, 24 , 14 , 33 );
System.out.println(localDateTime4);
LocalDate date = LocalDate.of( 2021 , 04 , 24 );
LocalTime time = LocalTime.of( 10 , 34 );
LocalDateTime localDateTime5
= LocalDateTime.of(date, time);
System.out.println(localDateTime5);
}
}
|
Output:
Example 3: Format LocalDateTime to string
To format a local time to the desired string representation, use the LocalDateTime.format(DateTimeFormatter) method.
Java
import java.time.LocalDateTime;
import java.time.format.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
DateTimeFormatter formatter
= DateTimeFormatter.ofPattern(
"yyyy-MM-dd HH:mm:ss a" );
LocalDateTime now = LocalDateTime.now();
String dateTimeString = now.format(formatter);
System.out.println(dateTimeString);
}
}
|
Output:
Note: In order to parse a string to LocalDateTime, convert time in a string to a local time instance, the LocalDateTime class has two overloaded parse() methods.
- parse(CharSequence text)
- parse(CharSequence text, DateTimeFormatter formatter)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...