Java LocalDate.atTime()
方法及示例
原文:https://www.studytonight.com/java-examples/java-localdate-attime-method-with-examples
atTime()
方法用于将该日期与时间相结合。它接受两个参数,并返回一个本地日期时间。
我们可以使用日期和时间的所有可能的有效组合来创建日期。下面给出了该方法的语法。
句法
public LocalDateTime atTime(int hour, int minute)
因素
小时 -一天中要使用的小时,从 0 到 23
分钟 -使用的小时分钟,从 0 到 59
返回
从该日期和指定时间形成的本地日期时间。
示例:
我们使用 LocalDate 类创建一个日期,然后使用atTime()
方法将时间和它结合起来。这个方法返回日期和时间,见下面的例子。
import java.time.LocalDate;
import java.time.LocalDateTime;
public class Demo {
public static void main(String[] args){
// Take a date
LocalDate localDate1 = LocalDate.of(2018, 2, 20);
// Displaying date
System.out.println("Date is : "+localDate1);
// using atTime() method
LocalDateTime localDateTime = localDate1.atTime(12,25);
System.out.println("Date with local time: "+localDateTime);
}
}
日期为:2018-02-20 日期与当地时间:2018-02-20T12:25
示例:
atTime()
方法有一个重载的方法,它采用了 LocalTime 类的一个实例。当我们想要创建一个全职的日期时,这很有用。见下面的例子。
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class Demo {
public static void main(String[] args){
// Take a date
LocalDate localDate1 = LocalDate.of(2018, 2, 20);
// Displaying date
System.out.println("Date is : "+localDate1);
// Getting a full time
LocalTime time = LocalTime.parse("12:10:20");
// using atTime() method
LocalDateTime localDateTime = localDate1.atTime(time);
System.out.println("Date with local time: "+localDateTime);
}
}
日期为:2018-02-20 日期与当地时间:2018-02-20T12:10:20
实时示例:
尝试一个真实的例子,用我们强大的在线 Java 编译器立即执行代码。