Java 8 – How to convert LocalTime in different formats ?

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

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,
    1. DateTimeFormatter.ofPattern(“HH:mm:ss“)
    2. DateTimeFormatter.ofPattern(“HH:mm:ss nnn“)
    3. DateTimeFormatter.ofPattern(“hh:mm:ss a“)
    4. DateTimeFormatter.ofPattern(“hh:mm a“)
    5. DateTimeFormatter.ofPattern(“HH:mm ss nnn“)
    6. DateTimeFormatter.ofPattern(“HH mm ss nnn“)
    7. 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,

  1. LocalTime in default format to (HH:mm:ss) format
  2. LocalTime in default format to (HH:mm:ss nnn) format
  3. LocalTime in default format to (hh:mm:ss a) format
  4. LocalTime in default format to (hh:mm a) format
  5. LocalTime in default format to (HH:mm ss nnn) format
  6. LocalTime in default format to (HH mm ss nnn) format
  7. 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:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalTime in different Format Style ?
Java 8 – How to convert String to LocalTime ?