Thursday, October 9, 2025
HomeLanguagesJavaLocalTime atOffset() method in Java with Examples

LocalTime atOffset() method in Java with Examples

The atOffset() method of a LocalTime class is used to combine this time with an offset object to create an OffsetTime object. All possible combinations of time and offset are valid.

Syntax:

public OffsetTime atOffset(ZoneOffset offset)

Parameters: This method accepts a single parameter offset which is the offset to combine with LocalTime Object, not null.

Return Value: This method returns the offset time formed from this time and the specified offset, not null.

Below programs illustrate the atOffset() method:

Program 1:




// Java program to demonstrate
// LocalTime.atOffset() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime Object
        LocalTime time
            = LocalTime.parse("16:12:49");
  
        // create a ZoneOffset object
        // with 7200 sec means 2 hours
        ZoneOffset offset
            = ZoneOffset.ofTotalSeconds(7200);
  
        // apply atOffset()
        OffsetTime offsettime
            = time.atOffset(offset);
  
        // print LocalDateTime
        System.out.println("Offset Time:"
                           + offsettime.toString());
    }
}


Output:

Offset Time:16:12:49+02:00

Program 2:




// Java program to demonstrate
// LocalTime.atOffset() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime Object
        LocalTime time
            = LocalTime.parse("17:52:49");
  
        // create a ZoneOffset object
        // with 3 hours 45 minutes
        ZoneOffset offset
            = ZoneOffset.ofHoursMinutes(3, 45);
  
        // apply atOffset()
        OffsetTime offsettime
            = time.atOffset(offset);
  
        // print LocalDateTime
        System.out.println("Offset Time:"
                           + offsettime.toString());
    }
}


Output:

Offset Time:17:52:49+03:45

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#atOffset(java.time.ZoneOffset)

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

Most Popular

Dominic
32348 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7095 POSTS0 COMMENTS
Thapelo Manthata
6791 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS