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("Initially 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());
}
}
输出
Initially 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)