61. LocalDateTime from LocalDate and LocalTime

The LocalDateTime class exposes a series of of() methods that are useful for obtaining a different kind of instance of LocalDateTime. For example, a LocalDateTime class that is obtained from the year, month, day, hour, minute, second, or nanosecond looks like this:

LocalDateTime ldt = LocalDateTime.of​(2020, 4, 1, 12, 33, 21, 675);

So, the preceding code combines date and time as arguments of the of() method. In order to combine date and time as objects, the solution can take advantage of the following of() method:

public static LocalDateTime of​(LocalDate date, LocalTime time)

This results in LocalDate and LocalTime, as in the following:

LocalDate localDate = LocalDate.now(); // 2019-Feb-24
LocalTime localTime = LocalTime.now(); // 02:08:10 PM

They can be combined in a single object, LocalDateTime, as follows:

LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);

Formatting LocalDateTime reveals the date and time as follows:

// 2019-Feb-24 02:08:10 PM
String localDateTimeAsString = localDateTime
.format(DateTimeFormatter.ofPattern("yyyy-MMM-dd hh:mm:ss a"));
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.29.119