Java中的 ZoneOffsetTransition of() 方法示例
Java.time.zone.ZoneOffsetTransition类的of()方法用于借助本地日期时间、前偏移和后偏移创建区域偏移过渡的对象。
句法:
public static ZoneOffsetTransition of(LocalDateTime transition,
ZoneOffset offsetBefore,
ZoneOffset offsetAfter)
参数:此方法将以下参数作为参数。
- 过渡:必须为其创建过渡的本地日期时间对象。
- offsetBefore:初始过渡的区域偏移对象。
- offsetAfter:最终过渡的区域偏移对象。
返回值:该方法通过本地日期时间、前偏移和后偏移返回区域偏移过渡的对象。
以下是说明of()方法的示例:
示例 1:
Java
// Java program to demonstrate
// of() method
import java.util.*;
import java.io.*;
import java.time.*;
import java.time.chrono.*;
import java.time.zone.*;
public class GFG {
public static void main(String[] argv)
{
// creating and initializing
// the object of LocalDateTime
LocalDateTime loc
= LocalDateTime.of(
1999, 04, 25,
03, 24, 45, 0);
// creating and initializing
// the object of ZoneOffset
ZoneOffset off1
= ZoneOffset.ofTotalSeconds(12);
// creating and initializing
// the object of ZoneOffset
ZoneOffset off2
= ZoneOffset.ofTotalSeconds(8);
// creating and initializing
// ZoneOffsetTransition Object
// by using of() method
ZoneOffsetTransition zonetrans1
= ZoneOffsetTransition.of(
loc, off1, off2);
// getting the string representation
// of this object
String str
= zonetrans1.toString();
// display the result
System.out.println(
"zone offset transition : "
+ str);
}
}
Java
// Java program to demonstrate
// of() method
import java.util.*;
import java.io.*;
import java.time.*;
import java.time.chrono.*;
import java.time.zone.*;
public class GFG {
public static void main(String[] argv)
{
// creating and initializing
// the object of LocalDateTime
LocalDateTime loc
= LocalDateTime.of(
2018, 04, 25,
03, 24, 45, 0);
// creating and initializing
// the object of ZoneOffset
ZoneOffset off1
= ZoneOffset.ofTotalSeconds(12);
// creating and initializing
// the object of ZoneOffset
ZoneOffset off2
= ZoneOffset.ofTotalSeconds(8);
// creating and initializing
// ZoneOffsetTransition Object
// by using of() method
ZoneOffsetTransition zonetrans1
= ZoneOffsetTransition.of(
loc, off1, off2);
// getting the string representation
// of this object
String str = zonetrans1.toString();
// display the result
System.out.println(
"zone offset transition : "
+ str);
}
}
输出:
区域偏移过渡:过渡[在 1999-04-25T03:24:45+00:00:12 到 +00:00:08 重叠]
区域偏移过渡:过渡[在 1999-04-25T03:24:45+00:00:12 到 +00:00:08 重叠]
示例 2:
Java
// Java program to demonstrate
// of() method
import java.util.*;
import java.io.*;
import java.time.*;
import java.time.chrono.*;
import java.time.zone.*;
public class GFG {
public static void main(String[] argv)
{
// creating and initializing
// the object of LocalDateTime
LocalDateTime loc
= LocalDateTime.of(
2018, 04, 25,
03, 24, 45, 0);
// creating and initializing
// the object of ZoneOffset
ZoneOffset off1
= ZoneOffset.ofTotalSeconds(12);
// creating and initializing
// the object of ZoneOffset
ZoneOffset off2
= ZoneOffset.ofTotalSeconds(8);
// creating and initializing
// ZoneOffsetTransition Object
// by using of() method
ZoneOffsetTransition zonetrans1
= ZoneOffsetTransition.of(
loc, off1, off2);
// getting the string representation
// of this object
String str = zonetrans1.toString();
// display the result
System.out.println(
"zone offset transition : "
+ str);
}
}
输出:
区域偏移过渡:过渡[在 2018-04-25T03:24:45+00:00:12 到 +00:00:08 重叠]
区域偏移过渡:过渡[在 2018-04-25T03:24:45+00:00:12 到 +00:00:08 重叠]
参考: https://docs.oracle.com/javase/9/docs/api/ Java/time/zone/ZoneOffsetTransition.html#of-java.time.LocalDateTime-java.time.ZoneOffset-java.time.ZoneOffset-