In this article, we will learn how to form LocalDateTime passing LocalDate & LocalTime using LocalDateTime.of() method provided in Java 1.8 version
Form LocalDateTime passing LocalDate & LocalTime :
- There are 7 variants of LocalDateTime.of() methods
- Out of 7 variants, one variant takes LocalDate & LocalTime as input-arguments and returns LocalDateTime as output
- LocalDateTime.of(LocalDate, LocalTime) –
- Obtains an instance of LocalDateTime from a date/LocalDate and time/LocalTime
- Finally, print LocalDateTime in different formatted style like SHORT & MEDIUM to the console
FormLocalDateTime2.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormLocalDateTime2 {
public static void main(String[] args) {
// 1. get current System Date
LocalDate localDate = LocalDate.now();
System.out.println("LocalDate :- " + localDate);
// 2. get current System Time
LocalTime localTime = LocalTime.now();
System.out.println("LocalTime :- " + localTime);
// 3. form LocalDateTime passing LocalDate(year/Month/day) & LocalTime(hour/minute/second/nano)
LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
System.out.println("\nLocalDateTime :- " + localDateTime);
// 3.1 format LocalDateTime in FormatStyle.SHORT
String formattedStr1 = localDateTime.format(
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT));
System.out.println("\nLocalDateTime in FormatStyle.SHORT :- " + formattedStr1);
// 3.2 format LocalDateTime in FormatStyle.MEDIUM
String formattedStr2 = localDateTime.format(
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM));
System.out.print("LocalDateTime in FormatStyle.MEDIUM :- " + formattedStr2);
}
}
Output:
LocalDate :- 2022-08-08
LocalTime :- 17:25:27.247754800
LocalDateTime :- 2022-08-08T17:25:27.247754800
LocalDateTime in FormatStyle.SHORT :- 08/08/22, 5:25 pm
LocalDateTime in FormatStyle.MEDIUM :- 08-Aug-2022, 5:25:27 pm
Related Articles:
- Java 8 – LocalDateTime with method details and examples
- Java 8 – How to get Date and Time fields from LocalDateTime ?
- Java 8 – How to form LocalDateTime passing Date and Time fields ?
- Java 8 – How to form LocalDateTime passing LocalDate and LocalTime ?
- Java 8 – How to form LocalDateTime passing Instant and ZoneId ?
- Java 8 – How to form LocalDateTime passing Second/Nano and ZoneOffset ?
- Java 8 – How to parse LocalDateTime in String form ?
- Java 8 – How to convert String to LocalDateTime ?
- Java 8 – How to convert LocalDateTime to String ?
- Java 8 – How to convert LocalDateTime in different formats ?
- Java 8 – How to convert LocalDateTime in different Format Style ?
- Java 8 – How to convert LocalDateTime to ZonedDateTime ?
- Java 8 – How to convert LocalDateTime to an OffsetDateTime ?
- Java 8 – How to convert LocalDateTime to an Instant ?
- Java 8 – How to extract LocalDateTime and LocalTime from LocalDateTime ?
- Java 8 – How to convert LocalDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDateTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDateTime in different ways ?
- Java 8 – How to add Date and Time fields to LocalDateTime ?
- Java 8 – How to subtract Date and Time fields from LocalDateTime ?
- Java 8 – How to alter Date and Time fields of LocalDateTime ?
- Java 8 – How to check whether a LocalDateTime is Before another LocalDateTime ?
- Java 8 – How to check whether a LocalDateTime is After another LocalDateTime ?
- Java 8 – How to compare two LocalDateTime instances ?
- Java 8 – How to find difference between two LocalDateTime using Period & Duration ?
- Java 8 – What are all the Temporal Fields supported by LocalDateTime ?
- Java 8 – What are all the Temporal Units supported by LocalDateTime ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Month.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/FormatStyle.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!