Java Convert LocalTime to String

Tags: Java LocalTime String Java 8 DateTimeFormatter

In this Java core tutorial we learn different solutions how to format a java.time.LocalTime object to a time String in Java programming language with different example Java programs.

Table of contents

  1. Format LocalTime using DateTimeFormatter with Custom Time Pattern
  2. Format LocalTime to String in Localized Time Pattern
  3. Format LocalTime using Predefined DateTimeFormatter Constants
  4. Convert LocalTime to String using toString() method
  5. More Java Examples of Format LocalTime to String using DateTimeFormatter

Format LocalTime using DateTimeFormatter with Custom Time Pattern

In Java we can use the DateTimeFormatter object to define a custom time pattern and use the LocalTime.format() to convert a LocalTime object to String. For example, in the following Java program we format a LocalTime object to String in time format pattern hh:mm:ss a

ConvertLocalTimeToStringExample1.java

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class ConvertLocalTimeToStringExample1 {
    public static void main(String... args) {
        int hour = 20;
        int minute = 30;
        int second = 45;

        LocalTime localTime = LocalTime.of(hour, minute, second);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
        String localTimeString = localTime.format(formatter);

        System.out.println(localTimeString);
    }
}
The output as below.
08:30:45 pm

Format LocalTime to String in Localized Time Pattern

With DateTimeFormatter class we can get a specified locale time format in different style using the DateTimeFormatter.ofLocalizedTime() method.

DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);

Below are the list of format styles we can use for LocalTime format

  • FormatStyle.MEDIUM to format medium text style such as 1:20:30 pm
  • FormatStyle.SHORT to format short text style such as 10:20 am

In the following Java code we show how to use DateTimeFormatter.ofLocalizedTime() method with FormatStyle.SHORT

ConvertLocalTimeToStringExample2.java

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class ConvertLocalTimeToStringExample2 {
    public static void main(String... args) {
        LocalTime localTime = LocalTime.of(10, 20, 30);
        DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
        String localTimeString = localTime.format(formatter);

        System.out.println(localTimeString);
    }
}
The output as below.
10:20 am

In the following Java code we show how to use DateTimeFormatter.ofLocalizedTime() method with FormatStyle.MEDIUM

ConvertLocalTimeToStringExample3.java

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class ConvertLocalTimeToStringExample3 {
    public static void main(String... args) {
        LocalTime localTime = LocalTime.of(13, 20, 30);
        DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);
        String localTimeString = localTime.format(formatter);

        System.out.println(localTimeString);
    }
}
The output as below.
1:20:30 pm

Format LocalTime using Predefined DateTimeFormatter Constants

The DateTimeFormatter class provides predefined constants which we can use to format a LocalTime object such as ISO_LOCAL_TIME , ISO_TIME.

In the following Java program we use DateTimeFormatter.ISO_LOCAL_TIME to format a LocalTime object to String.

ConvertLocalTimeToStringExample4.java

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class ConvertLocalTimeToStringExample4 {
    public static void main(String... args) {
        LocalTime localTime = LocalTime.of(13, 20, 30);
        DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_TIME;
        String localTimeString = localTime.format(formatter);

        System.out.println(localTimeString);
    }
}
The output as below.
13:20:30

In the following Java program we use DateTimeFormatter.ISO_TIME to format a LocalTime object to String.

ConvertLocalTimeToStringExample5.java

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class ConvertLocalTimeToStringExample5 {
    public static void main(String... args) {
        LocalTime localTime = LocalTime.of(13, 20, 30);
        DateTimeFormatter formatter = DateTimeFormatter.ISO_TIME;
        String localTimeString = localTime.format(formatter);

        System.out.println(localTimeString);
    }
}
The output as below.
13:20:30

Convert LocalTime to String using toString() method

In Java we can simply use the LocalTime.toString() method to convert a LocalTime object to String as Java code below.

ConvertLocalTimeToStringExample6.java

import java.time.LocalTime;

public class ConvertLocalTimeToStringExample6 {
    public static void main(String... args) {
        LocalTime localTime = LocalTime.of(13, 20, 30);
        String localTimeString = localTime.toString();

        System.out.println(localTimeString);
    }
}
The output as below.
13:20:30

More Java Examples of Format LocalTime to String using DateTimeFormatter

In the following Java program we show more example Java code of how to use custom time format pattern to convert LocalTime object to time String.

ConvertLocalTimeToStringExample7.java

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class ConvertLocalTimeToStringExample7 {
    public static void main(String... args) {
        LocalTime localTime = LocalTime.of(21, 30, 50, 199887766);

        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("HH:mm");
        String localTimeString1 = localTime.format(formatter1);

        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("HH:mm:ss");
        String localTimeString2 = localTime.format(formatter2);

        DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
        String localTimeString3 = localTime.format(formatter3);

        DateTimeFormatter formatter4 = DateTimeFormatter.ofPattern("HH:mm:ss.SSSSSSSSS");
        String localTimeString4 = localTime.format(formatter4);

        DateTimeFormatter formatter5 = DateTimeFormatter.ofPattern("hh:mm:ss a");
        String localTimeString5 = localTime.format(formatter5);

        DateTimeFormatter formatter6 = DateTimeFormatter.ofPattern("K:mm a");
        String localTimeString6 = localTime.format(formatter6);

        System.out.println("HH:mm -> " + localTimeString1);
        System.out.println("HH:mm:ss -> " + localTimeString2);
        System.out.println("HH:mm:ss.SSS -> " + localTimeString3);
        System.out.println("HH:mm:ss.SSSSSSSSS -> " + localTimeString4);
        System.out.println("hh:mm:ss a -> " + localTimeString5);
        System.out.println("K:mm a -> " + localTimeString6);
    }
}
The output as below.
HH:mm -> 21:30
HH:mm:ss -> 21:30:50
HH:mm:ss.SSS -> 21:30:50.199
HH:mm:ss.SSSSSSSSS -> 21:30:50.199887766
hh:mm:ss a -> 09:30:50 pm
K:mm a -> 9:30 pm

Happy Coding 😊

Java Convert LocalTime to Seconds of Day

Java Convert LocalTime to Nanos of Day

Java Convert LocalTime to Epoch Seconds

Java Convert LocalTime to LocalDateTime

Java Convert LocalTime to OffsetTime

Java Convert LocalTime to Date

Java Convert LocalTime to Calendar

Java LocalTime by Examples

Java LocalDate by Examples

Java LocalDateTime by Examples

Java Date by Examples

Java Calendar by Examples

Java Calendar using Calendar.Builder by Examples