In this article, we will learn how to convert LocalTime in different formats using Java 1.8 version or in short how to convert LocalTime into String-form
For String to LocalTime conversion, read Java 8 – How to convert String to LocalTime ?
1. Convert LocalTime in different formats :
- We can convert default ISO_LOCAL_TIME format (HH:mm:ss.nnn) to any other formats using LocalTime.format() method by passing DateTimeFormatter as argument with required pattern
- In below illustration, we are using 7 different custom formats/patterns as listed below,
- DateTimeFormatter.ofPattern(“HH:mm:ss“)
- DateTimeFormatter.ofPattern(“HH:mm:ss nnn“)
- DateTimeFormatter.ofPattern(“hh:mm:ss a“)
- DateTimeFormatter.ofPattern(“hh:mm a“)
- DateTimeFormatter.ofPattern(“HH:mm ss nnn“)
- DateTimeFormatter.ofPattern(“HH mm ss nnn“)
- DateTimeFormatter.ofPattern(“hh mm ss nnn a“)
- Note: If the specified custom format/pattern isn’t in correct form then DateTimeParseException is thrown
2. LocalTime examples in different formats :
Let see different examples for converting LocalTime in default format (HH:mm:ss.nnn) to customized format as listed below,
- LocalTime in default format to (HH:mm:ss) format
- LocalTime in default format to (HH:mm:ss nnn) format
- LocalTime in default format to (hh:mm:ss a) format
- LocalTime in default format to (hh:mm a) format
- LocalTime in default format to (HH:mm ss nnn) format
- LocalTime in default format to (HH mm ss nnn) format
- LocalTime in default format to (hh mm ss nnn a) format
2.1 LocalTime in default format to (HH:mm:ss) format :
- Default LocalTime format is (HH:mm:ss.nnn)
- Converted String format is (HH:mm:ss)
FormatLocalTimeExample1.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class FormatLocalTimeExample1 {
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.ofPattern("HH:mm:ss");
// 3. LocalTime to (HH:mm:ss) format in String form
String str = localTime.format(dateTimeFormatter);
System.out.print("\nLocalTime to (HH:mm:ss) format :- \n" + str);
}
}
Output:
Current System Time is :-
19:45:25.862919200
LocalTime to (HH:mm:ss) format :-
19:45:25
2.2 LocalTime in default format to (HH:mm:ss nnn) format :
- Default LocalTime format is (HH:mm:ss.nnn)
- Converted String format is (HH:mm:ss nnn)
FormatLocalTimeExample2.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class FormatLocalTimeExample2 {
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.ofPattern("HH:mm:ss nnn");
// 3. LocalTime to (HH:mm:ss nnn) format in String form
String str = localTime.format(dateTimeFormatter);
System.out.print("\nLocalTime to (HH:mm:ss nnn) format :- \n" + str);
}
}
Output:
Current System Time is :-
19:45:48.134820700
LocalTime to (HH:mm:ss nnn) format :-
19:45:48 134820700
2.3 LocalTime in default format to (hh:mm:ss a) format :
- Default LocalTime format is (HH:mm:ss.nnn)
- Converted String format is (hh:mm:ss a)
FormatLocalTimeExample3.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class FormatLocalTimeExample3 {
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.ofPattern("hh:mm:ss a");
// 3. LocalTime to (hh:mm:ss a) format in String form
String str = localTime.format(dateTimeFormatter);
System.out.print("\nLocalTime to (hh:mm:ss a) format :- \n" + str);
}
}
Output:
Current System Time is :-
19:48:19.848907800
LocalTime to (hh:mm:ss a) format :-
07:48:19 pm
2.4 LocalTime in default format to (hh:mm a) format :
- Default LocalTime format is (HH:mm:ss.nnn)
- Converted String format is (hh:mm a)
FormatLocalTimeExample4.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class FormatLocalTimeExample4 {
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.ofPattern("hh:mm a");
// 3. LocalTime to (hh:mm a) format in String form
String str = localTime.format(dateTimeFormatter);
System.out.print("\nLocalTime to (hh:mm a) format :- \n" + str);
}
}
Output:
Current System Time is :-
19:47:54.504314900
LocalTime to (hh:mm a) format :-
07:47 pm
2.5 LocalTime in default format to (HH:mm ss nnn) format :
- Default LocalTime format is (HH:mm:ss.nnn)
- Converted String format is (HH:mm ss nnn)
FormatLocalTimeExample5.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class FormatLocalTimeExample5 {
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.ofPattern("HH:mm ss nnn");
// 3. LocalTime to (HH:mm ss nnn) format in String form
String str = localTime.format(dateTimeFormatter);
System.out.print("\nLocalTime to (HH:mm ss nnn) format :- \n" + str);
}
}
Output:
Current System Time is :-
19:48:37.914274600
LocalTime to (HH:mm ss nnn) format :-
19:48 37 914274600
2.6 LocalTime in default format to (HH mm ss nnn) format :
- Default LocalTime format is (HH:mm:ss.nnn)
- Converted String format is (HH mm ss nnn)
FormatLocalTimeExample6.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class FormatLocalTimeExample6 {
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.ofPattern("HH mm ss nnn");
// 3. LocalTime to (HH mm ss nnn) format in String form
String str = localTime.format(dateTimeFormatter);
System.out.print("\nLocalTime to (HH mm ss nnn) format :- \n" + str);
}
}
Output:
Current System Time is :-
19:49:02.871098900
LocalTime to (HH mm ss nnn) format :-
19 49 02 871098900
2.7 LocalTime in default format to (hh mm ss nnn a) format :
- Default LocalTime format is (HH:mm:ss.nnn)
- Converted String format is (hh mm ss nnn a)
FormatLocalTimeExample7.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class FormatLocalTimeExample7 {
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.ofPattern("hh mm ss nnn a");
// 3. LocalTime to (hh mm ss nnn a) format in String form
String str = localTime.format(dateTimeFormatter);
System.out.print("\nLocalTime to (hh mm ss nnn a) format :- \n" + str);
}
}
Output:
Current System Time is :-
19:49:31.939035600
LocalTime to (hh mm ss nnn a) format :-
07 49 31 939035600 pm
3. Throws DateTimeParseException for Invalid format :
- If the specified pattern/format in invalid then DateTimeParseException is thrown
- In the below illustration, capital-letter ‘M‘ is used for Minute which is wrong
- Time notation,
- Capital-letter ‘H‘ should be used for 24-hour Hour format
- Small-letter ‘h‘ should be used for 12-hour Hour format
- Small-letter ‘m‘ should be used for Minute
- Small-letter ‘s‘ should be used for Second
- Small-letter ‘n‘ should be used for Nanosecond
FormatLocalTimeException.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class FormatLocalTimeException {
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.ofPattern("HH:MM:ss");
// 3. LocalTime to (HH:MM:ss) format in String form
String str = localTime.format(dateTimeFormatter);
System.out.print("\nLocalTime to (HH:MM:ss) format :- \n" + str);
}
}
Output:
Current System Time is :-
09:56:52.491989300
Exception in thread "main" java.time.temporal.UnsupportedTemporalTypeException:
Unsupported field: MonthOfYear
at java.base/java.time.LocalTime.get0(LocalTime.java:703)
at java.base/java.time.LocalTime.getLong(LocalTime.java:680)
at java.base/java.time.format.DateTimePrintContext.getValue(DateTimePrintContext.java:308)
at java.base/java.time.format.DateTimeFormatterBuilder$NumberPrinterParser
.format(DateTimeFormatterBuilder.java:2763)
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.FormatLocalTimeException
.main(FormatLocalTimeException.java:20)
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/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!