當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。