w3resource

Java: Display combine local date and time in a single object


41. Combine Local Date and Time

Write a Java program to combine local date and time into 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
                .format(DateTimeFormatter.ofPattern("yyyy-MMM-dd"));
        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:

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



For more Practice: Solve these Related Problems:

  • Write a Java program to merge a LocalDate object and a LocalTime object into a LocalDateTime instance.
  • Write a Java program to combine separate date and time variables into one date-time object and format it.
  • Write a Java program to create a LocalDateTime from given date and time inputs and display the result.
  • Write a Java program to accept date and time inputs separately and then combine them into a single object.

Go to:


PREV : Date Only and Time Only.
NEXT : Define Period and Duration.

Java Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.