Saturday, November 22, 2025
HomeLanguagesJavaOffsetTime from() method in Java with examples

OffsetTime from() method in Java with examples

The from() method of OffsetTime class in Java obtains an instance of OffsetTime from a temporal object which is passed in the parameter of the method.

Syntax:

public static OffsetTime from(TemporalAccessor temporal)

Parameter: This method accepts a single mandatory parameter temporal which specifies the temporal object to be converted and it is not null.

Return Value: It returns the offset time and not null.

Below programs illustrate the from() method:

Program 1 :




// Java program to demonstrate the from() method
  
import java.time.OffsetTime;
import java.time.ZonedDateTime;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Function called to get current time
        OffsetTime time
            = OffsetTime.from(ZonedDateTime.now());
  
        // Prints the current time
        System.out.println("Current-time: "
                           + time);
    }
}


Output:

Current-time: 13:07:59.941Z

Program 2 :




// Java program to demonstrate the from() method
  
import java.time.OffsetTime;
import java.time.ZonedDateTime;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Function called to get current time
        OffsetTime time
            = OffsetTime.from(ZonedDateTime.now());
  
        // Prints the current time
        System.out.println("Current-time: "
                           + time);
    }
}


Output:

Current-time: 13:08:03.087Z

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#from-java.time.temporal.TemporalAccessor-

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6785 POSTS0 COMMENTS
Nicole Veronica
11932 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6864 POSTS0 COMMENTS
Umr Jansen
6852 POSTS0 COMMENTS