java timezone_Java TimeZone getOffset()方法与示例

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值