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


Java OffsetTime atDate()用法及代码示例


Java中OffsetTime类的atDate()方法将此时间与日期结合起来以创建OffsetDateTime。

用法:

public OffsetDateTime atDate(LocalDate date)

参数:此方法接受单个参数date,该参数指定要与之合并的日期,而不是null。


返回值:它返回从此时间和指定日期形成的OffsetDateTime,不为null

以下示例程序旨在说明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-



相关用法


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