java timezone
TimeZone类的getOffset()方法 (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()方法在java.util包中可用。
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(int age's,int yy,int mm,int dd,int dow,int ms)方法用于返回该模式为夏令时时更新的当前日期的时区偏移量。
getOffset(long dd) method is used to return the offset of this time zone which is calculated from UTC at the given date (dd).
getOffset(long dd)方法用于返回该时区的偏移量,该偏移量是根据给定日期(dd)的UTC计算的。
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)"
在第一种情况下, getOffset(int age's,int yy,int mm,int dd,int dow,int ms)“
- int era's – represents the era of the specified date.
- int age's –代表指定日期的时代。
- int yy – represents the year of the specified date.
- int yy –表示指定日期的年份。
- int mm – represents the month of the specified date.
- int mm –表示指定日期的月份。
- int dd – represents the day of week of the specified date.
- int dd –表示指定日期的星期几。
- int dow – represents the year of the specified date.
- int dow –表示指定日期的年份。
- int ms – represents the milliseconds in Standard Local Time.
- int ms –表示标准本地时间的毫秒数。
In the second case, getOffset(long dd),
在第二种情况下, getOffset(long dd) ,
- long dd – represents dates and it is represented in milliseconds.
- long dd –代表日期,以毫秒为单位。
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.
在这两种情况下,方法的返回类型均为int –返回以ms为单位的偏移量,以添加默认时区(GMT)以返回本地时间。
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
翻译自: https://www.includehelp.com/java/timezone-getoffset-method-with-example.aspx
java timezone