In this article, we will learn how to parse LocalTime and java.util.Date in String form using LocalTime.parse() method provided in the Java 1.8 version
1. Parse java.time.LocalTime :
- Sometimes, we need to parse time passed in String-form to LocalTime, for that we can use LocalTime.parse() method which will return LocalTime in the default (HH:mm:ss.nnn) format
- There are 2 variants of LocalTime.parse() method –
- LocalTime.parse(CharSequence text) – returns LocalTime in the default (HH:mm:ss.nnn) format
- LocalTime.parse(CharSequence text, DateTimeFormatter formatter) – this variant helps to customize LocalTime in different formats using formatter
- Both variants throws java.time.format.DateTimeParseException when,
- For the 1st variant, if the specified time in string-form isn’t in the default formats as listed below
- HH:mm:ss.nnn
- HH:mm:ss
- HH:mm
- For the 2nd variant, if specified time in string-form isn’t matching with custom pattern/format specified in the DateTimeFormatter
- For the 1st variant, if the specified time in string-form isn’t in the default formats as listed below
- Finally, print LocalTime to the console for both the variants
ParseLocalTime.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class ParseLocalTime {
public static void main(String[] args) {
// 1. convert/parse timeInString to LocalTime - hour/minute/second/nano
String officeStartTime = "08:29:59.987654321";
LocalTime startTime = LocalTime.parse(officeStartTime);
System.out.println("Parsed Start-Time (hour/minute/second/nano) is :- \n" + startTime);
// 2. convert/parse timeInString to LocalTime - hour/minute/second/nano
String officeEndTime = "05:30:01.123456789";
LocalTime endTime = LocalTime.parse(officeEndTime);
System.out.println("\nParsed End-Time (hour/minute/second/nano) is :- \n" + endTime);
// 3. convert/parse timeInString to LocalTime - only hour/minute/second
String time3 = "11:39:47";
LocalTime localTime3 = LocalTime.parse(time3);
System.out.println("\nParsed Time (hour/minute/second) is :- \n" + localTime3);
// 4. convert/parse timeInString to LocalTime - only hour/minute
String time4 = "10:59";
LocalTime localTime4 = LocalTime.parse(time4);
System.out.println("\nParsed Time (hour/minute) is :- \n" + localTime4);
// 5. convert/parse timeInString to LocalTime - hour/minute/second/nano
String time5 = "08:29 59 987654321";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm ss nnn");
LocalTime localTime5 = LocalTime.parse(time5, dateTimeFormatter);
System.out.print("\nParsed/formatted LocalTime is :- \n" + localTime5);
}
}
Output:
Parsed Start-Time (hour/minute/second/nano) is :-
08:29:59.987654321
Parsed End-Time (hour/minute/second/nano) is :-
05:30:01.123456789
Parsed Time (hour/minute/second) is :-
11:39:47
Parsed Time (hour/minute) is :-
10:59
Parsed/formatted LocalTime is :-
08:29:59.987654321
2. Parse java.util.Date :
- Default format for java.util.Date is E MMM dd HH:mm:ss zzz yyyy
- Parsing Date in custom formats is simple using DateFormat & SimpleDateFormat classes as shown in the below example
- Finally, print Date to the console for both default and custom formats
ParseDateTime.java
package in.bench.resources.java8.localtime.examples;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ParseDateTime {
public static void main(String[] args) {
// 1. current date
Date date = new Date();
System.out.println("Date/Time in "
+ "default format :- \n" + date);
// 2. format
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String str = dateFormat.format(date);
System.out.print("\nFormatted Date/Time in "
+ "(dd-MM-yyyy HH:mm:ss) format :- \n" + str);
}
}
Output:
Date/Time in default format :-
Wed Aug 03 20:10:55 IST 2022
Formatted Date/Time in (dd-MM-yyyy HH:mm:ss) format :-
03-08-2022 20:10:55
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/8/docs/api/java/time/format/FormatStyle.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 !!