Java ZoneOffsetTransition tutorial with examples

PreviousNext

A transition between two offsets caused by a discontinuity in the local time-line.

Introduction

A transition between two offsets caused by a discontinuity in the local time-line.

A transition between two offsets is normally the result of a daylight savings cutover.

The discontinuity is normally a gap in spring and an overlap in autumn.

ZoneOffsetTransition models the transition between the two offsets.

Gaps occur where there are local date-times that simply do not exist.

An example would be when the offset changes from +03:00 to +04:00.

This might be described as 'the clocks will move forward one hour tonight at 1am'.

Overlaps occur where there are local date-times that exist twice.

An example would be when the offset changes from +04:00 to +03:00.

This might be described as 'the clocks will move back one hour tonight at 2am'.

Example

The following code shows how to use ZoneOffsetTransition from java.time.zone.

Example 1

// Java program to demonstrate
// getInstant() method

import java.util.*;
import java.io.*;
import java.time.*;
import java.time.chrono.*;
import java.time.zone.*;

public class Main {
    public static void main(String[] argv) {

        // creating and initializing
        // the object of LocalDateTime
        LocalDateTime loc = LocalDateTime.of(1999, 04, 25, 03, 24, 45, 0);

        // creating and initializing
        // the object of ZoneOffset
        ZoneOffset off1 = ZoneOffset.ofTotalSeconds(8);

        // creating and initializing
        // the object of ZoneOffset
        ZoneOffset off2 = ZoneOffset.ofTotalSeconds(12);

        // creating and initializing
        // ZoneOffsetTransition Object
        ZoneOffsetTransition zonetrans1 = ZoneOffsetTransition.of(loc, off1, off2);

        // getting the instant between two transition
        // by using getInstant() method
        Instant inst = zonetrans1.getInstant();

        // display the result
        System.out.println("Instant : " + inst);
    }/*  w w   w . d   e m   o   2s   .   c o m  */
}

Result

Example 2

// Java program to demonstrate
// getInstant() method

import java.util.*;
import java.io.*;
import java.time.*;
import java.time.chrono.*;
import java.time.zone.*;

public class Main {
    public static void main(String[] argv) {

        // creating and initializing
        // the object of LocalDateTime
        LocalDateTime loc = LocalDateTime.of(1999, 04, 25, 03, 24, 45, 0);

        // creating and initializing
        // the object of ZoneOffset
        ZoneOffset off1 = ZoneOffset.ofTotalSeconds(12);

        // creating and initializing
        // the object of ZoneOffset
        ZoneOffset off2 = ZoneOffset.ofTotalSeconds(8);

        // creating and initializing
        // ZoneOffsetTransition Object
        ZoneOffsetTransition zonetrans1 = ZoneOffsetTransition.of(loc, off1, off2);

        // getting the instant between two transition
        // by using getInstant() method
        Instant inst = zonetrans1.getInstant();

        // display the result
        System.out.println("Instant : " + inst);
    }//   w w  w  .   d   e m  o   2  s . c  o  m  
}

Result

Example 3

// Java program to demonstrate
// getOffsetAfter() method

import java.util.*;
import java.io.*;
import java.time.*;
import java.time.chrono.*;
import java.time.zone.*;

public class Main {
    public static void main(String[] argv) {

        // Creating and initializing the
        // object of LocalDateTime
        LocalDateTime loc = LocalDateTime.of(1999, 04, 25, 03, 24, 45, 0);

        // Creating and initializing the
        // object of ZoneOffset
        ZoneOffset off1 = ZoneOffset.ofTotalSeconds(8);

        // Creating and initializing the
        // object of ZoneOffset
        ZoneOffset off2 = ZoneOffset.ofTotalSeconds(12);

        // Creating and initializing
        // ZoneOffsetTransition Object
        ZoneOffsetTransition zonetrans1 = ZoneOffsetTransition.of(loc, off1, off2);

        // Getting the offset after value
        // by using getOffsetAfter() method
        ZoneOffset off = zonetrans1.getOffsetAfter();

        // Display the result
        System.out.println("Zoneoffset after transition : " + off);
    }//    w  w w   .d   e m   o  2 s    .  co    m
}

Result

PreviousNext

Related