Java offsetTime atDate()方法及示例
Java中OffsetTime类的 atDate() 方法将这个时间与一个日期结合起来,创建一个OffsetDateTime。
语法:
public OffsetDateTime atDate(LocalDate date)
参数: 该方法接受一个参数 date ,指定与之结合的日期,不为空。
返回值: 它返回由这个时间和指定日期形成的OffsetDateTime,不为空。
下面的程序说明了atDate()方法。
程序1 :
// Java program to demonstrate the atDate() method
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
public class GFG {
public static void main(String[] args)
{
// parses the current date
LocalDate date = LocalDate.now();
System.out.println("Current date: " + date);
// Parses the current time
OffsetTime time = OffsetTime.parse("11:10:10+06:03");
OffsetDateTime datetime = time.atDate(date);
System.out.println("Current date and time: " + datetime);
}
}
输出。
Current date: 2018-12-31
Current date and time: 2018-12-31T11:10:10+06:03
程序2 :
// Java program to demonstrate the atDate() method
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
public class GFG {
public static void main(String[] args)
{
// parses the current date
LocalDate date = LocalDate.now();
System.out.println("Current date: " + date);
// Parses the current time
OffsetTime time = OffsetTime.parse("12:15:14+16:03");
OffsetDateTime datetime = time.atDate(date);
System.out.println("Current date and time: " + datetime);
}
}
输出。
Current date: 2018-12-31
Current date and time: 2018-12-31T12:15:14+16:03
参考资料 : https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#atDate-java.time.LocalDate-