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

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
    1. FormatStyle.SHORT
    2. FormatStyle.MEDIUM
    3. FormatStyle.LONG
    4. FormatStyle.FULL
  • In below illustration, we are using above provided in-built formats to format LocalTime as listed below,
    1. DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
    2. DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);
    3. DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG);
    4. DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL);
  • Note: for creating above formats we need DateTimeFormatter

2. LocalTime examples in different Format Style :

  1. Convert LocalTime in default format to FormatStyle.SHORT format
  2. Convert LocalTime in default format to FormatStyle.MEDIUM format
  3. Convert LocalTime in default format to FormatStyle.LONG format
  4. 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:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalTime to LocalDateTime ?
Java 8 – How to convert LocalTime in different formats ?