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,
- String to Date conversion in different formats
- Date to String conversion is also required for reverse process
- For LocalTime to String conversion, read Java 8 – How to convert LocalTime to String ?
1. String to LocalTime conversion :
We will use below formats to convert String to LocalTime
- HH:mm:ss.nnn (default)
- hh:mm:ss.nnn a
- HH:mm:ss (default)
- hh:mm:ss a
- HH:mm (default)
- 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,
- HH:mm:ss.nnn
- HH:mm:ss
- 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:
- Java 8 – LocalTime with method details and examples
- Java 8 – How to get Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to form LocalTime passing Hour, Minute and Second fields ?
- Java 8 – How to parse LocalTime in String form ?
- Java 8 – How to convert String to LocalTime ?
- Java 8 – How to convert LocalTime to String ?
- Java 8 – How to convert LocalTime in different formats ?
- Java 8 – How to convert LocalTime in different Format Style ?
- Java 8 – How to convert LocalTime to LocalDateTime ?
- Java 8 – How to convert LocalTime to ZonedDateTime ?
- Java 8 – How to convert LocalTime to an OffsetDateTime ?
- Java 8 – How to convert LocalTime to an Instant ?
- Java 8 – How to convert LocalTime to an OffsetTime ?
- Java 8 – How to convert LocalTime to Seconds and vice-versa ?
- Java 8 – How to convert LocalTime to Nanoseconds and vice-versa ?
- Java 8 – How to convert LocalTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime in different ways ?
- Java 8 – How to add Hour, Minute and Second fields to LocalTime ?
- Java 8 – How to subtract Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to alter Hour, Minute and Second fields of LocalTime ?
- Java 8 – How to check whether a LocalTime is Before another LocalTime ?
- Java 8 – How to check whether a LocalTime is After another LocalTime ?
- Java 8 – How to compare two LocalTime instances ?
- Java 8 – How to find time duration between two LocalTime instances ?
- Java 8 – What are all the Temporal Fields supported by LocalTime ?
- Java 8 – What are all the Temporal Units supported by LocalTime ?
- Java 9 – Find difference between two LocalTime instances upto nanosecond precision ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!