gpt4 book ai didi

java - 在Java中创建日期对象最有效的方法是什么

转载 作者:行者123 更新时间:2023-12-01 16:57:36 24 4
gpt4 key购买 nike

我正在java中创建Date对象。

第一种方式: 通过使用日历

Date today = Calendar.getInstance().getTime(); 

第二种方式:

带有日期类

Date today1 = new Date();

哪一种是最有效的方法?

最佳答案

从 Java 8 开始,您应该选择 LocalDateTime 或 LocalDate:

LocalDateTime timePoint = LocalDateTime.now(
); // The current date and time
LocalDate.of(2012, Month.DECEMBER, 12); // from values

LocalDate theDate = timePoint.toLocalDate(); // or
theDate = LocalDate.now(); //

来自文档:

[For example, the existing classes (such as java.util.Date and SimpleDateFormatter) aren’t thread-safe, leading to potential concurrency issues for users—not something the average developer would expect to deal with when writing date-handling code.

Some of the date and time classes also exhibit quite poor API design. For example, years in java.util.Date start at 1900, months start at 1, and days start at 0—not very intuitive.]1

关于java - 在Java中创建日期对象最有效的方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30504516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com