Tuesday, June 16, 2026
HomeLanguagesJavaLocalTime from() method in Java with Examples

LocalTime from() method in Java with Examples

The from() method of a LocalTime class helps to get instance of LocalTime from TemporalAccessor object passed as parameter to method. A TemporalAccessor represents an arbitrary set of date and time information and this method helps to get a LocalTime based on the specified TemporalAccessor object. 

Syntax:

public static LocalTime from(TemporalAccessor temporal)

Parameters: This method accepts a single parameter temporal which is the temporal object. It should not be null. 

Return value: This method returns the local time from temporal object, not null 

Exception: This method throws a DateTimeException if unable to convert to a LocalTime. 

Below programs illustrate the from() method: 

Program 1: 

Java




// Java program to demonstrate
// LocalTime.from() method
 
import java.time.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // create a ZonedDateTime object
        ZonedDateTime zonedDateTime
            = ZonedDateTime.now();
 
        // apply from()
        LocalTime value
            = LocalTime.from(zonedDateTime);
 
        // print result
        System.out.println("LocalTime value : "
                           + value);
    }
}


Output:

LocalTime value : 06:17:32.760

Program 2: 

Java




// Java program to demonstrate
// LocalTime.from() method
 
import java.time.*;
 
public class GFG {
    public static void main(String[] args)
    {
        // create a OffsetDateTime object
        OffsetDateTime offset
            = OffsetDateTime.now();
 
        // apply from()
        LocalTime value = LocalTime.from(offset);
 
        // print result
        System.out.println("LocalTime value : "
                           + value);
    }
}


Output:

LocalTime value : 06:17:35.131

References: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#from(java.time.temporal.TemporalAccessor)

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS