Java DateTime, Calendar Exercises: Display combine local date and time in a single object

Java DateTime, Calendar: Exercise-41 with Solution

Write a Java program to display combine local date and time in a single object.

Sample Solution:

Java Code:

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

public class Main {
    public static void main(String[] args) {                       
        LocalDate local_Dt = LocalDate.now();
        String localDateAsString = local_Dt
        System.out.println("Local Date: " + localDateAsString);        
        LocalTime local_Time = LocalTime.now();
        String localTimeAsString = local_Time
                .format(DateTimeFormatter.ofPattern("hh:mm:ss a"));
        System.out.println("Local Time: " + localTimeAsString);        
        LocalDateTime localDateTime = LocalDateTime.of(local_Dt, local_Time);        
        String localDateTimeAsString = localDateTime
                .format(DateTimeFormatter.ofPattern("yyyy-MMM-dd hh:mm:ss a"));        
        System.out.println("\nCombine local Date Time: " + localDateTimeAsString);

Sample Output:

Local Date: 2019-Nov-16
Local Time: 08:21:24 AM

Combine local Date Time: 2019-Nov-16 08:21:24 AM

N.B.: The value may be changed accroding to your system date and time.


Flowchart: Java DateTime, Calendar Exercises - Display combine local date and time in a single object

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to display current date without time and current time without date.
Next: Write a Java program to define a period of time using date-based values (Period) and a duration of time using time-based values (Duration).

What is the difficulty level of this exercise?

Java: Tips of the Day

Easiest way to convert a List to a Set in Java:

Set<Foo> foo = new HashSet<Foo>(myList);

Ref: https://bit.ly/3qIJSTZ