Java 8 – How to parse LocalTime in String form ?

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 –
    1. LocalTime.parse(CharSequence text) – returns LocalTime in the default (HH:mm:ss.nnn) format
    2. 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,
    1. 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
    2. For the 2nd variant, if specified time in string-form isn’t matching with custom pattern/format specified in the DateTimeFormatter
  • 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:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to convert String to LocalTime ?
Java 8 – How to form LocalTime passing Hour, Minute and Second fields ?