Java8- Learn LocalDateTime Basics with Example
What is java.time.LocalDateTime in java8?
LocalDateTime
is introduced in java8 language as part of Date and Time API enhancement.
It represents Date
and time
without timezone
information. Example LocalDateTime object displays as 2022-12-14T06:12:40.
It is an immutable
object, which means once the object is created, will not be modified.
It is used to store dates like birthdays and holidays with time information.
LocalDate
contains only year, month, Day, hour, minute, and second with nano-precision.
Here is the LocalDateTime Syntax and Signature
This class implements ChronoLocalDateTime
of LocalDate
interface.
public final class LocalDateTime
implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable
Let’s see examples LocalDateTime class.
How to get the Current Date and Time in java?
There are multiple ways we can get the current date and time in java.
- using now() method
now()
method return the date and time using default system time zone.
import java.time.LocalDateTime;
public class LocalDateTimeExample {
public static void main(String[] args) {
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println(currentDateTime); //2021-12-14T18:13:40.564
}
}
- using of() method
It has a static of()
method, takes java.time.LocalDate and java.time.LocalTime to create LocalDateTime object.
Here is an example of a method usage.
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class LocalDateTimeExample {
public static void main(String[] args) {
LocalDateTime currentDateTime = LocalDateTime.of(LocalDate.now(), LocalTime.now());
System.out.println(currentDateTime);
}
}
How to Convert String Date to LocalDateTime in java?
The LocalDateTime
has parse
method which accepts string object and DateTimeFormatter
instance.
The datetimeformatter object created with the date pattern
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class OptionalClassExample {
public static void main(String[] args) {
String string = "2022-12-12 12:30";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime localDateTime = LocalDateTime.parse(string, dateTimeFormatter);
System.out.println(localDateTime); //2022-12-12 12:30
}
}
How to Convert LocalDateTime to java.util.Date?
The date
is a legacy class and LocalDateTime
introduces in the java8 version. Sometimes, We need to convert LocalDateTime to Date class.
Here is an example
import java.time.LocalDateTime;
import java.util.Date;
public class OptionalClassExample {
public static void main(String[] args) {
LocalDateTime localDateTime =LocalDateTime.now();
Date date = java.sql.Timestamp.valueOf( localDateTime );
System.out.println(date);
}
}
How to Convert LocalDateTime to Instant?
It provides ofInstant()
method which takes instant object and zone id - system default zone id LocalDateTime.toInstant()
method is used to Convert LocalDateTime to Instant.
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class OptionalClassExample {
public static void main(String[] args) {
Instant instant = Instant.now();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
System.out.println(localDateTime);
}
}
How to Create LocalDatetime from Epcoh milliseconds?
System.currentTimeMillis()
returns current Epcoh
milliseconds. Create Instant
using Instant.ofEpochMilli()
method, convert Instant
to LocalDate
time.
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.TimeZone;
public class OptionalClassExample {
public static void main(String[] args) {
LocalDateTime localDateTime =
LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), TimeZone
.getDefault().toZoneId());
System.out.println(localDateTime);
}
}
Conclusion
This tutorial is about the LocalDateTime tutorial with examples.