Home »
Java programming language
Java TimeZone getOffset() Method with Example
TimeZone Class getOffset() method: Here, we are going to learn about the getOffset() method of TimeZone Class with its syntax and example.
Submitted by Preeti Jain, on March 13, 2020
TimeZone Class getOffset() method
Syntax:
public abstract 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 return the time zone offset for current date updated when the mode is daylight savings time.
- getOffset(long dd) method is used to return the offset of this time zone which is calculated from UTC at the given date (dd).
- These methods don't throw an exception at the time of getting offset.
- These are non-static methods and it is accessible with the class object only and if we try to access these methods with the 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 returns offset in ms to add default time zone (GMT) to return local time.
Example:
// Java program to demonstrate the example
// of getOffset() method of TimeZone
import java.util.*;
public class GetOffsetOfTimeZone {
public static void main(String args[]) {
// Instantiates TimeZone object
TimeZone tz = TimeZone.getTimeZone("Africa/Asmera");
// Display tz
System.out.println("tz: " + tz);
// By using getOffset() method is to
// get the offset according the given
// arguments
System.out.print("tz.getOffset(era's, yy, mm,dd,dow,time): ");
System.out.println(tz.getOffset(1, 2009, 3, 3, 3, 100));
// By using getOffset() method is to
// get the offset according the given
// date
System.out.print("tz.getOffset(long d): ");
System.out.println(tz.getOffset(Calendar.ZONE_OFFSET));
}
}
Output
tz: sun.util.calendar.ZoneInfo[id="Africa/Asmera",offset=10800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]
tz.getOffset(era's, yy, mm,dd,dow,time): 10800000
tz.getOffset(long d): 10800000