In this article, we will learn how to format LocalTime in different Format Style provided in Java 1.8 version
1. Format LocalTime in different Format Style :
- FormatStyle class provides 4 different enum constants for formatting LocalTime, those are
- FormatStyle.SHORT
- FormatStyle.MEDIUM
- FormatStyle.LONG
- FormatStyle.FULL
- In below illustration, we are using above provided in-built formats to format LocalTime as listed below,
- DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
- DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);
- DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG);
- DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL);
- Note: for creating above formats we need DateTimeFormatter
2. LocalTime examples in different Format Style :
- Convert LocalTime in default format to FormatStyle.SHORT format
- Convert LocalTime in default format to FormatStyle.MEDIUM format
- Convert LocalTime in default format to FormatStyle.LONG format
- Convert LocalTime in default format to FormatStyle.FULL format
2.1 LocalTime in default format to FormatStyle.SHORT format :
- This format style converts LocalTime in default (HH:mm:ss.nnn) format to (HH:mm a) format ignoring second & nanosecond fields
FormatLocalTimeStyleShort.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormatLocalTimeStyleShort {
public static void main(String[] args) {
// 1. get Current System Time
LocalTime localTime = LocalTime.now();
System.out.println("Current System Time is :- \n" + localTime);
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofLocalizedTime(FormatStyle.SHORT);
// 3. LocalTime in default format to FormatStyle.SHORT format in String-form
String str = localTime.format(dateTimeFormatter);
System.out.print("\nLocalTime in FormatStyle.SHORT format :- \n" + str);
}
}
Output:
Current System Time is :-
23:15:12.315050800
LocalTime in FormatStyle.SHORT format :-
11:15 pm
2.2 LocalTime in default format to FormatStyle.MEDIUM format :
- This format style converts LocalTime in default (HH:mm:ss.nnn) format to (HH:mm:ss a) format ignoring nanosecond field
FormatLocalTimeStyleMedium.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormatLocalTimeStyleMedium {
public static void main(String[] args) {
// 1. get Current System Time
LocalTime localTime = LocalTime.now();
System.out.println("Current System Time is :- \n" + localTime);
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofLocalizedTime(FormatStyle.MEDIUM);
// 3. LocalTime in default format to FormatStyle.MEDIUM format in String-form
String str = localTime.format(dateTimeFormatter);
System.out.print("\nLocalTime in FormatStyle.MEDIUM format :- \n" + str);
}
}
Output:
Current System Time is :-
23:17:21.949595800
LocalTime in FormatStyle.MEDIUM format :-
11:17:21 pm
2.3 LocalTime in default format to FormatStyle.LONG format :
- While converting LocalTime in default format to LONG style format throws runtime exception stating “Zone information is not available“
- So, it should be strictly used when we are dealing with Zone information like ZonedDateTime
FormatLocalTimeStyleLong.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormatLocalTimeStyleLong {
public static void main(String[] args) {
// 1. get Current System Time
LocalTime localTime = LocalTime.now();
System.out.println("Current System Time is :- \n" + localTime);
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofLocalizedTime(FormatStyle.LONG);
// 3. LocalTime in default format to FormatStyle.LONG format in String-form
String str = localTime.format(dateTimeFormatter);
System.out.print("\nLocalTime in FormatStyle.LONG format :- \n" + str);
}
}
Output:
Current System Time is :-
23:20:13.888293300
Exception in thread "main" java.time.DateTimeException:
Unable to extract ZoneId from temporal 23:20:13.888293300 with chronology ISO
at java.base/java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:289)
at java.base/java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser
.format(DateTimeFormatterBuilder.java:4142)
at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser
.format(DateTimeFormatterBuilder.java:2402)
at java.base/java.time.format.DateTimeFormatterBuilder$LocalizedPrinterParser
.format(DateTimeFormatterBuilder.java:4844)
at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser
.format(DateTimeFormatterBuilder.java:2402)
at java.base/java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1849)
at java.base/java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1823)
at java.base/java.time.LocalTime.format(LocalTime.java:1436)
at in.bench.resources.java8.localtime.examples.FormatLocalTimeStyleLong
.main(FormatLocalTimeStyleLong.java:22)
2.4 LocalTime in default format to FormatStyle.FULL format :
- This is very much similar like above example which requires Zone information otherwise throws runtime exception stating “Zone information is not available” during conversion from LocalTime in default format to FULL style format
- So, it should be strictly used when we are dealing with Zone information like ZonedDateTime
FormatLocalTimeStyleFull.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class FormatLocalTimeStyleFull {
public static void main(String[] args) {
// 1. get Current System Time
LocalTime localTime = LocalTime.now();
System.out.println("Current System Time is :- \n" + localTime);
// 2. DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter
.ofLocalizedTime(FormatStyle.FULL);
// 3. LocalTime in default format to FormatStyle.FULL format in String-form
String str = localTime.format(dateTimeFormatter);
System.out.print("\nLocalTime in FormatStyle.FULL format :- \n" + str);
}
}
Output:
Current System Time is :-
23:21:59.898125100
Exception in thread "main" java.time.DateTimeException:
Unable to extract ZoneId from temporal 23:21:59.898125100 with chronology ISO
at java.base/java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:289)
at java.base/java.time.format.DateTimeFormatterBuilder$ZoneTextPrinterParser
.format(DateTimeFormatterBuilder.java:4142)
at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser
.format(DateTimeFormatterBuilder.java:2402)
at java.base/java.time.format.DateTimeFormatterBuilder$LocalizedPrinterParser
.format(DateTimeFormatterBuilder.java:4844)
at java.base/java.time.format.DateTimeFormatterBuilder$CompositePrinterParser
.format(DateTimeFormatterBuilder.java:2402)
at java.base/java.time.format.DateTimeFormatter.formatTo(DateTimeFormatter.java:1849)
at java.base/java.time.format.DateTimeFormatter.format(DateTimeFormatter.java:1823)
at java.base/java.time.LocalTime.format(LocalTime.java:1436)
at in.bench.resources.java8.localtime.examples.FormatLocalTimeStyleFull
.main(FormatLocalTimeStyleFull.java:22)
Related Articles:
- Java 8 – LocalTime with method details and examples
- Java 8 – How to get Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to form LocalTime passing Hour, Minute and Second fields ?
- Java 8 – How to parse LocalTime in String form ?
- Java 8 – How to convert String to LocalTime ?
- Java 8 – How to convert LocalTime to String ?
- Java 8 – How to convert LocalTime in different formats ?
- Java 8 – How to convert LocalTime in different Format Style ?
- Java 8 – How to convert LocalTime to LocalDateTime ?
- Java 8 – How to convert LocalTime to ZonedDateTime ?
- Java 8 – How to convert LocalTime to an OffsetDateTime ?
- Java 8 – How to convert LocalTime to an Instant ?
- Java 8 – How to convert LocalTime to an OffsetTime ?
- Java 8 – How to convert LocalTime to Seconds and vice-versa ?
- Java 8 – How to convert LocalTime to Nanoseconds and vice-versa ?
- Java 8 – How to convert LocalTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime in different ways ?
- Java 8 – How to add Hour, Minute and Second fields to LocalTime ?
- Java 8 – How to subtract Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to alter Hour, Minute and Second fields of LocalTime ?
- Java 8 – How to check whether a LocalTime is Before another LocalTime ?
- Java 8 – How to check whether a LocalTime is After another LocalTime ?
- Java 8 – How to compare two LocalTime instances ?
- Java 8 – How to find time duration between two LocalTime instances ?
- Java 8 – What are all the Temporal Fields supported by LocalTime ?
- Java 8 – What are all the Temporal Units supported by LocalTime ?
- Java 9 – Find difference between two LocalTime instances upto nanosecond precision ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/FormatStyle.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!