Java 8 – How to convert String to LocalTime ?

In this article, we will learn how to convert String to LocalTime in Java 1.8 version using different time formats

Q) What is the need of converting String to LocalTime ?

  • Generally, whenever we receive any data from web application then it is passed in the form of String
  • But for further processing we need to convert String to LocalTime
  • This article explains about String to LocalTime conversion in different formats

Read also,

1. String to LocalTime conversion :

We will use below formats to convert String to LocalTime

  1. HH:mm:ss.nnn (default)
  2. hh:mm:ss.nnn a
  3. HH:mm:ss (default)
  4. hh:mm:ss a
  5. HH:mm (default)
  6. hh:mm a

Note: for creating above formats we need DateTimeFormatter

2. Convert String to LocalTime examples :

When the given time in String is in the following acceptable/default formats, then there is no need to format using DateTimeFormatter,

  1. HH:mm:ss.nnn
  2. HH:mm:ss
  3. HH:mm

For every other formats, formatting is required using DateTimeFormatter

2.1 Convert String in (HH:mm:ss.nnn) format to LocalTime :

  • Given String is in (HH:mm:ss.nnn) format that’s need to be converted into LocalTime
  • As the given String is in default/acceptable format so there is no need of DateTimeFormatter for conversion of String to LocalTime
  • Finally, print LocalTime to the console

ConvertStringToLocalTime1.java

package in.bench.resources.java8.localtime.examples;

import java.time.LocalTime;

public class ConvertStringToLocalTime1 {

	public static void main(String[] args) {

		// 1. string
		String strTime = "16:28:13.858695400";


		// 2. parse time in String to default LocalTime format
		LocalTime localDate = LocalTime.parse(strTime);


		// 3. print to console
		System.out.println("Original String :- \n" + strTime);
		System.out.print("\nString in default (HH:mm:ss.nnn) format to LocalTime :- \n"
				+ localDate);
	}
}

Output:

Original String :- 
16:28:13.858695400

String in default (HH:mm:ss.nnn) format to LocalTime :- 
16:28:13.858695400

2.2 Convert String in (hh:mm:ss.nnn a) format to LocalTime :

  • Given String is in (hh:mm:ss.nnn a) format that’s need to be converted into LocalTime
  • For parsing the given String, DateTimeFormatter with matching pattern is required as shown below,
    • DateTimeFormatter.ofPattern(“hh:mm:ss.nnn a“);
  • Time notation,
    • Capital-letter ‘H‘ is used for 24-hour format
    • Small-letter ‘h‘ is used for 12-hour format
  • Finally, print LocalTime to the console

ConvertStringToLocalTime2.java

package in.bench.resources.java8.localtime.examples;

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

public class ConvertStringToLocalTime2 {

	public static void main(String[] args) {

		// 1. string
		String strTime = "11:28:13.858695400 pm";


		// 2. DateTimeFormatter - (h) used for 12 hour format
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss.nnn a");


		// 3. parse time in String format using above dateTimeFormatter
		LocalTime localDate = LocalTime.parse(strTime, dateTimeFormatter);


		// 4. print to console
		System.out.println("Original String :- \n" + strTime);
		System.out.print("\nString in (HH:mm:ss.nnn a) format to LocalTime :- \n"
				+ localDate);
	}
}

Output:

Original String :- 
11:28:13.858695400 pm

String in (HH:mm:ss.nnn a) format to LocalTime :- 
23:28:13.858695400

2.3 Convert String in (HH:mm:ss) format to LocalTime :

  • Given String is in (HH:mm:ss) format that’s need to be converted into LocalTime
  • As the given String is in default/acceptable format so there is no need of DateTimeFormatter for conversion of String to LocalTime

ConvertStringToLocalDate3.java

package in.bench.resources.java8.localtime.examples;

import java.time.LocalTime;

public class ConvertStringToLocalTime3 {

	public static void main(String[] args) {

		// 1. string
		String strTime = "16:28:13";


		// 2. parse time in String format using above dateTimeFormatter
		LocalTime localDate = LocalTime.parse(strTime);


		// 3. print to console
		System.out.println("Original String :- \n" + strTime);
		System.out.print("\nString in default (HH:mm:ss) format to LocalTime :- \n"
				+ localDate);
	}
}

Output:

Original String :- 
16:28:13

String in default (HH:mm:ss) format to LocalTime :- 
16:28:13

2.4 Convert String in (hh:mm:ss a) format to LocalTime :

  • Given String is in (HH:mm:ss a) format that’s need to be converted into LocalTime
  • For parsing the given String, DateTimeFormatter with matching pattern is required as shown below,
    • DateTimeFormatter.ofPattern(“hh:mm:ss a“);
  • Finally, print LocalTime to the console

ConvertStringToLocalTime4.java

package in.bench.resources.java8.localtime.examples;

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

public class ConvertStringToLocalTime4 {

	public static void main(String[] args) {

		// 1. string
		String strTime = "11:28:13 pm";


		// 2. DateTimeFormatter - (h) used for 12 hour format
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss a");


		// 3. parse time in String format using above dateTimeFormatter
		LocalTime localDate = LocalTime.parse(strTime, dateTimeFormatter);


		// 4. print to console
		System.out.println("Original String :- \n" + strTime);
		System.out.print("\nString in (HH:mm:ss a) format to LocalTime :- \n"
				+ localDate);
	}
}

Output:

Original String :- 
11:28:13 pm

String in (HH:mm:ss a) format to LocalTime :- 
23:28:13

2.5 Convert String in (HH:mm) format to LocalTime :

  • Given String is in (HH:mm) format that’s need to be converted into LocalTime
  • As the given String is in default/acceptable format so there is no need of DateTimeFormatter for conversion of String to LocalTime

ConvertStringToLocalTime5.java

package in.bench.resources.java8.localtime.examples;

import java.time.LocalTime;

public class ConvertStringToLocalTime5 {

	public static void main(String[] args) {

		// 1. string
		String strTime = "16:28";


		// 2. parse time in String format using above dateTimeFormatter
		LocalTime localDate = LocalTime.parse(strTime);


		// 3. print to console
		System.out.println("Original String :- \n" + strTime);
		System.out.print("\nString in default (HH:mm) format to LocalTime :- \n"
				+ localDate);
	}
}

Output:

Original String :- 
16:28

String in default (HH:mm) format to LocalTime :- 
16:28

2.6 Convert String in (hh:mm a) format to LocalTime :

  • Given String is in (hh:mm a) format that’s need to be converted into LocalTime
  • For parsing the given String, DateTimeFormatter with matching pattern is required as shown below,
    • DateTimeFormatter.ofPattern(“hh:mm a“);
  • Finally, print LocalTime to the console

ConvertStringToLocalTime6.java

package in.bench.resources.java8.localtime.examples;

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

public class ConvertStringToLocalTime6 {

	public static void main(String[] args) {

		// 1. string
		String strTime = "11:28 pm";


		// 2. DateTimeFormatter - (h) used for 12 hour format
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm a");


		// 3. parse time in String format using above dateTimeFormatter
		LocalTime localDate = LocalTime.parse(strTime, dateTimeFormatter);


		// 4. print to console
		System.out.println("Original String :- \n" + strTime);
		System.out.print("\nString in (HH:mm a) format to LocalTime :- \n"
				+ localDate);
	}
}

Output:

Original String :- 
11:28 pm

String in (HH:mm a) format to LocalTime :- 
23:28

3. DateTimeParseException:

  • If the specified String doesn’t match with the specified pattern/format then DateTimeParseException is thrown
  • In the below illustration, capital-letter ‘M‘ is used for minute instead small-letter ‘m‘ and due to this an exception is raised during runtime stating “Unable to obtain LocalTime from TemporalAccessor: {HourOfDay=23, MicroOfSecond=858695, SecondOfMinute=13, MonthOfYear=28, MilliOfSecond=858, NanoOfSecond=858695400},ISO of type java.time.format.Parsed
  • Time notation,
    • Capital-letter ‘H‘ should be used for 24-hour Hour fromat
    • 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

ConvertStringToLocalTimeException.java

package in.bench.resources.java8.localtime.examples;

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

public class ConvertStringToLocalTimeException {

	public static void main(String[] args) {

		// 1. string
		String strTime = "11:28:13.858695400 pm";


		// 2. DateTimeFormatter - (h) used for 12 hour format
		DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("hh:MM:ss.nnn a");


		// 3. parse time in String format using above dateTimeFormatter
		LocalTime localDate = LocalTime.parse(strTime, dateTimeFormatter);


		// 4. print to console
		System.out.println("Original String :- \n" + strTime);
		System.out.println("\nString in (HH:mm:ss.nnn a) format to LocalTime :- \n"
				+ localDate);
	}
}

Output:

Exception in thread "main" java.time.format.DateTimeParseException: 
Text '11:28:13.858695400 pm' could not be parsed: Unable to obtain LocalTime from TemporalAccessor: 
{HourOfDay=23, MicroOfSecond=858695, SecondOfMinute=13, MonthOfYear=28, MilliOfSecond=858, 
NanoOfSecond=858695400},ISO of type java.time.format.Parsed
	at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2023)
	at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1958)
	at java.base/java.time.LocalTime.parse(LocalTime.java:465)
	at in.bench.resources.java8.localtime.examples.ConvertStringToLocalTimeException
.main(ConvertStringToLocalTimeException.java:19)
Caused by: java.time.DateTimeException: Unable to obtain LocalTime from TemporalAccessor: 
{HourOfDay=23, MicroOfSecond=858695, SecondOfMinute=13, MonthOfYear=28, MilliOfSecond=858, 
NanoOfSecond=858695400},ISO of type java.time.format.Parsed
	at java.base/java.time.LocalTime.from(LocalTime.java:433)
	at java.base/java.time.format.Parsed.query(Parsed.java:241)
	at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954)
	... 2 more

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert LocalTime in different formats ?
Java 8 – How to parse LocalTime in String form ?