Create LocalDateTime from LocalDate and LocalTime in Java

In this post, we will write a program that builds a LocalDateTime from LocalDate and LocalTime. Combine date and time in a single LocalDateTime object.
Check out Java 8 examples at Java 8 Examples

Build a LocalDateTime from LocalDate and LocalTime

package net.sourcecodeexamples.java;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * Java Create LocalDateTime from LocalDate and LocalTime
 * @author sourcecodeexamples.net
 *
 */
public class Main {

    public static void main(String[] args) {

        LocalDate localDate = LocalDate.now();
        String localDateAsString = localDate.format(DateTimeFormatter.ofPattern("yyyy-MMM-dd"));
        System.out.println("LocalDate: " + localDateAsString);

        LocalTime localTime = LocalTime.now();
        String localTimeAsString = localTime.format(DateTimeFormatter.ofPattern("hh:mm:ss a"));
        System.out.println("LocalTime: " + localTimeAsString);

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

        String localDateTimeAsString = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MMM-dd hh:mm:ss a"));

        System.out.println("LocalDateTime: " + localDateTimeAsString);
    }
}

Output

LocalDate: 2020-Aug-11
LocalTime: 02:40:49 PM
LocalDateTime: 2020-Aug-11 02:40:49 PM

Related Posts


Comments