Java: Display combine local date and time in a single object
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:
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?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics