Home » Java programming language

Java SimpleTimeZone getOffset() Method with Example

SimpleTimeZone Class getOffset() method: Here, we are going to learn about the getOffset() method of SimpleTimeZone Class with its syntax and example.
Submitted by Preeti Jain, on March 12, 2020

SimpleTimeZone Class getOffset() method

Syntax:

    public int getOffset(int era's , int yy, int mm, int dd, int DOW, int ms);
    public int getOffset(long dd);
  • getOffset() method is available in java.util package.
  • getOffset(int era's , int yy, int mm, int dd, int dow, int ms) method is used to get the variations in milliseconds between local time and UTC.
  • getOffset(long dd) method is used to return the offset of this simple time zone from UTC at the given date (dd).
  • These methods may throw an exception at the time of getting offset.
    IllegalArgumentException: This exception may throw when anyone of the parameter is not in a range.
  • These are non-static methods and it is accessible with class object only and if we try to access these methods with class name then we will get an error.

Parameter(s):

  • In the first case, getOffset(int era's , int yy, int mm, int dd, int dow, int ms)"
    • int era's – represents the era of the specified date.
    • int yy – represents the year of the specified date.
    • int mm – represents the month of the specified date.
    • int dd – represents the day of week of the specified date.
    • int dow – represents the year of the specified date.
    • int ms – represents the milliseconds in Standard Local Time.
  • In the second case, getOffset(long dd),
    • long dd – represents dates and it is represented in milliseconds.

Return value:

In both the cases, the return type of the method is int - It adds returned milliseconds to UTC to get local time.

Example:

// Java program to demonstrate the example 
// of getOffset() method of SimpleTimeZone

import java.util.*;

public class GetOffsetOfSimpleTimeZone {
    public static void main(String args[]) {
        // Instantiates SimpleTimeZone object
        SimpleTimeZone s_tz = new SimpleTimeZone(360, "FRANCE");

        // Display s_tz
        System.out.println("s_tz: " + s_tz);

        // By using getOffset() method is to
        // get the offset according the given arguments
        System.out.print("s_tz.getOffset(era's, yy, mm,dd,dow,time): ");
        System.out.println(s_tz.getOffset(1, 2009, 3, 3, 3, 100));

        // By using getOffset() method is to
        // get the offset according the given date
        System.out.print("s_tz.getOffset(long d): ");
        System.out.println(s_tz.getOffset(Calendar.ZONE_OFFSET));
    }
}

Output

s_tz: java.util.SimpleTimeZone[id=FRANCE,offset=360,dstSavings=3600000,useDaylight=false,startYear=0,startMode=0,startMonth=0,startDay=0,startDayOfWeek=0,startTime=0,startTimeMode=0,endMode=0,endMonth=0,endDay=0,endDayOfWeek=0,endTime=0,endTimeMode=0]
s_tz.getOffset(era's, yy, mm,dd,dow,time): 360
s_tz.getOffset(long d): 360


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.