Close

Java Date Time - LocalTime.parse() Examples

Java Date Time Java Java API 


Class:

java.time.LocalTime

java.lang.Objectjava.lang.Objectjava.time.LocalTimejava.time.LocalTimejava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.lang.ComparableComparablejava.io.SerializableSerializableLogicBig

Methods:

public static LocalTime parse(CharSequence text)

Returns the LocalTime instance for the provided text. The text must be valid per DateTimeFormatter.ISO_LOCAL_TIME.


public static LocalTime parse(CharSequence text,

DateTimeFormatter formatter)

Returns the LocalTime instance for the provided text and formatter. The provided text must be valid per DateTimeFormatter instance.



Examples


package com.logicbig.example.localtime;

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

public class ParseExample {

public static void main (String... args) {
LocalTime t = LocalTime.parse("21:22");
System.out.println(t);

t = LocalTime.parse("20:40", DateTimeFormatter.ISO_TIME);
System.out.printf("ISO_TIME: %s%n",t);

t = LocalTime.parse("02:30",
DateTimeFormatter.ISO_LOCAL_TIME);
System.out.printf("ISO_LOCAL_TIME: %s%n",t);
}
}

Output

21:22
ISO_TIME: 20:40
ISO_LOCAL_TIME: 02:30




package com.logicbig.example.localtime;

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

public class ParseExample2 {

public static void main (String... args) {
LocalTime t = LocalTime.parse(
"22 11 30", DateTimeFormatter.ofPattern("HH mm ss"));
System.out.println(t);

t = LocalTime.parse(
"10 11 30 PM", DateTimeFormatter.ofPattern("hh mm ss a"));
System.out.println(t);
}
}

Output

22:11:30
22:11:30




See Also