1. Using LocalTime.parse() method
2. Using DateTimeFormatter
Read Also: Convert String to LocalDateTime in Java
Let's dive deep into the topic:
Convert String to LocalTime in Java
1. Using LocalTime.parse() method
We can easily convert String to LocalTime in Java with the help of the LocalTime.parse() method as shown below in the example. According to Oracle docs, the string must represent a valid time and is parsed using DateTimeFormatter.ISO_LOCAL_TIME. LocalTime.parse() method will throw DateTimeParseException if the string can not be parsed.
Note: Time notation: Capital-letter 'H' must be used for 24hr format whereas small-letter 'h' must be used for 12hr format.
Small-letter 'm' must be used for a minute.
Small-letter 's' must be used for a second.
Small-letter 'n' must be used for nano-of-second. Check the complete list here.
Small-letter 'm' must be used for a minute.
Small-letter 's' must be used for a second.
Small-letter 'n' must be used for nano-of-second. Check the complete list here.
import java.time.LocalTime; public class StringToLocalTime { public static void main(String args[]) { String str1 = "05:20"; System.out.println("Converted HH:mm to LocalTime: " + LocalTime.parse(str1)); String str2 = "05:20:20"; System.out.println("Converted HH:mm:ss to LocalTime: " + LocalTime.parse(str2)); String str3 = "05:20:20."; System.out.println("Converted HH:mm:ss. to LocalTime: " + LocalTime.parse(str3)); String str4 = "05:20:20.1"; System.out.println("Converted HH:mm:ss.SSS to LocalTime: " + LocalTime.parse(str4)); String str5 = "05:20:20.12"; System.out.println("Converted HH:mm:ss.SSS to LocalTime: " + LocalTime.parse(str5)); String str6 = "05:20:20.234556666"; System.out.println("Converted HH:mm:ss.nnn to LocalTime: " + LocalTime.parse(str6)); } }
Output:
Converted HH:mm to LocalTime: 05:20
Converted HH:mm:ss to LocalTime: 05:20:20
Converted HH:mm:ss. to LocalTime: 05:20:20
Converted HH:mm:ss.SSS to LocalTime: 05:20:20.100
Converted HH:mm:ss.SSS to LocalTime: 05:20:20.120
Converted HH:mm:ss.nnn to LocalTime: 05:20:20.234556666
2. Using DateTimeFormatter
We can easily parse a string given in the format such as "hh:mm:ss.nnn a", "hh:mm:ss.SS a", "hh:mm:ss a", or "hh:mm a" where a represents AM-PM of day to LocalTime using DateTimeFormatter as shown below in the example:
import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.Locale; public class StringToLocalTime2 { public static void main(String args[]) { String str1 = "06:20 AM"; DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm a", Locale.US); System.out.println("Converted String to LocalTime: " + LocalTime.parse(str1, dateTimeFormatter)); String str2 = "06:20:20 AM"; dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss a", Locale.US); System.out.println("Converted String to LocalTime: " + LocalTime.parse(str2, dateTimeFormatter)); String str3 = "06:20:20. AM"; dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss. a", Locale.US); System.out.println("Converted String to LocalTime: " + LocalTime.parse(str3, dateTimeFormatter)); String str4 = "05:20:20.1 PM"; dateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss.S a", Locale.US); System.out.println("Converted String to LocalTime: " + LocalTime.parse(str4, dateTimeFormatter)); String str5 = "05:20:20.12 PM"; dateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss.SS a", Locale.US); System.out.println("Converted String to LocalTime: " + LocalTime.parse(str5, dateTimeFormatter)); String str6 = "05:20:20.234556666 PM"; dateTimeFormatter = DateTimeFormatter.ofPattern("hh:mm:ss.nnn a", Locale.US); System.out.println("Converted String to LocalTime: " + LocalTime.parse(str6, dateTimeFormatter)); } }
Output:
Converted String to LocalTime: 06:20
Converted String to LocalTime: 06:20:20
Converted String to LocalTime: 06:20:20
Converted String to LocalTime: 17:20:20.100
Converted String to LocalTime: 17:20:20.120
Converted String to LocalTime: 17:20:20.234556666
That's all for today. Please mention in the comments if you have any questions related to how to convert String to LocalTime in Java (with/ without nanoOfSeconds).