In this Java 8 example, we will take a look at how to format and display time in 12-hour AM PM format,
package org.code2care;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* Display Time in Java 8 in AM PM Format
*/
public class Java8TimeAmPMFormat {
public static void main(String[] args) {
DateTimeFormatter dateFormat12hourAmPM;
dateFormat12hourAmPM = DateTimeFormatter.ofPattern("hh:mm a");
LocalTime localTime = LocalTime.now();
System.out.println(dateFormat12hourAmPM.format(localTime));
}
}
Output:
08:12 AM
You need to use lower case hh to display time in 0-12 hours, but if you do not use a you will not get AM/PM displayed and the time will be hard to read, hence use hh:mm a
More Posts related to Java,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- How to Uninstall Brew on Mac - MacOS
- Java: Convert Byte to Binary String Example - Java
- How to install Python 2.7.x version on macOS Ventura/Sonoma - Python
- Command to Sort File In Reverse Order [Unix/Linux/macOS] - Bash
- HTML5 HELLO WORLD Example - Html
- Chessboard with pieces using pure HTML and CSS - Html
- Informal written computer correspondence acronyms list and meanings - 2022
- How to find version of Cargo in Rust - Rust