当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java TimeZone setRawOffset()用法及代码示例


Java中TimeZone类的setRawOffset(int offsetMillis)方法用于将基本时区偏移量设置为GMT。此偏移值可以是UTC以获取本地时间。

用法:

public abstract void 
    setRawOffset(int offsetMillis)

参数:该方法接受整数类型的单个参数offsetMillis,该参数指定给定的基准时区与GMT的偏移量。


返回值:该方法不返回任何值。

以下示例程序旨在说明上述函数:
示例1:

// Program to demonstrate the 
// setRawOffset() 
  
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Create time zone object 
        TimeZone obj 
            = TimeZone.getTimeZone("Pacific/Pago_Pago"); 
  
        // Printing RawOffset value 
        System.out.println("Initially RawOffset is = "
                           + obj.getRawOffset()); 
  
        // Setting RawOffset on object obj 
        obj.setRawOffset(6000000); 
        System.out.println("RawOffset "
                           + "set to 6000000"); 
  
        // Printing RawOffset value 
        System.out.println("Current RawOffset is = "
                           + obj.getRawOffset()); 
    } 
}
输出:
Initially RawOffset is = -39600000
RawOffset set to 6000000
Current RawOffset is = 6000000

示例2:

// Program to demonstrate the 
// setRawOffset() 
  
import java.util.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // Create time zone object 
        TimeZone obj 
            = TimeZone.getTimeZone("Europe/Rome"); 
  
        // Printing RawOffset value 
        System.out.println("Inittally RawOffset is = "
                           + obj.getRawOffset()); 
  
        // Setting RawOffset on object obj 
        obj.setRawOffset(8000000); 
        System.out.println("RawOffset "
                           + "set to 8000000"); 
  
        // Printing RawOffset value 
        System.out.println("Current RawOffset is = "
                           + obj.getRawOffset()); 
    } 
}
输出:
Inittally RawOffset is = 3600000
RawOffset set to 8000000
Current RawOffset is = 8000000

参考:https://docs.oracle.com/javase/7/docs/api/java/util/TimeZone.html#setRawOffset(int)



相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 TimeZone setRawOffset() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。