Saturday, August 30, 2025
HomeLanguagesJavaZonedDateTime withFixedOffsetZone() method in Java with Examples

ZonedDateTime withFixedOffsetZone() method in Java with Examples

The withFixedOffsetZone() method of a ZonedDateTime class is used to return a copy of this ZonedDateTime object with the zone ID set to the offset. This method returns a ZonedDateTime where the zone ID is the same as getOffset().The local date-time, offset and instant of the returned ZonedDateTime will be the same as in this date-time.

Syntax:

public ZonedDateTime withFixedOffsetZone()

Parameters: This method accepts no parameters.

Return value: This method returns a ZonedDateTime with the zone ID set to the offset.

Below programs illustrate the withFixedOffsetZone() method:

Program 1:




// Java program to demonstrate
// ZonedDateTime.withFixedOffsetZone() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a ZonedDateTime object
        ZonedDateTime zonedDT
            = ZonedDateTime
                  .ofLocal(
                      LocalDateTime.of(2018, 11, 4, 1, 25, 43),
                      ZoneId.of("US/Central"),
                      ZoneOffset.ofHours(-6));
  
        // print ZonedDateTime
        System.out.println("Before withFixedOffsetZone(): "
                           + zonedDT);
  
        // apply withFixedOffsetZone()
        ZonedDateTime zonedDT2
            = zonedDT.withFixedOffsetZone();
  
        // print ZonedDateTime after withFixedOffsetZone()
        System.out.println("After withFixedOffsetZone(): "
                           + zonedDT2);
    }
}


Output:

Before withFixedOffsetZone(): 2018-11-04T01:25:43-06:00[US/Central]
After withFixedOffsetZone(): 2018-11-04T01:25:43-06:00

Program 2:




// Java program to demonstrate
// ZonedDateTime.withFixedOffsetZone() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a ZonedDateTime object
        ZonedDateTime zonedDT
            = ZonedDateTime.now();
  
        // print ZonedDateTime
        System.out.println("Before withFixedOffsetZone(): "
                           + zonedDT);
  
        // apply withFixedOffsetZone()
        ZonedDateTime zonedDT2
            = zonedDT.withFixedOffsetZone();
  
        // print ZonedDateTime after withFixedOffsetZone()
        System.out.println("After withFixedOffsetZone(): "
                           + zonedDT2);
    }
}


Output:

Before withFixedOffsetZone(): 2018-12-17T05:16:41.417Z[Etc/UTC]
After withFixedOffsetZone(): 2018-12-17T05:16:41.417Z

Reference: https://docs.oracle.com/javase/10/docs/api/java/time/ZonedDateTime.html#withFixedOffsetZone()

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

Most Popular

Dominic
32250 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11792 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11840 POSTS0 COMMENTS
Shaida Kate Naidoo
6733 POSTS0 COMMENTS
Ted Musemwa
7014 POSTS0 COMMENTS
Thapelo Manthata
6689 POSTS0 COMMENTS
Umr Jansen
6704 POSTS0 COMMENTS