我正在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/