tl;dr
LocalDate.now()
.toString()
2016-01-07
最好总是明确规定你所期望/期望的时间区。
LocalDate.now( ZoneId.of( "America/Montreal" ) )
.toString()
Avoid old date-time classes
The java.util. 现在,你使用的日历级是遗产。 避免这一类以及java。 日期和日期。 事实证明,这些问题设计不好,混淆不清,麻烦多。
java.time
旧的日间班由java.time 。 框架载于 Java8和随后。 大部分功能已退回到Java 6 &7功能载于。 三个Ten-Backport项目,并进一步适应。 3TenABP。
这些现代班子可以在一条单一的法典线上实现你的目标,如下文所示。
Date-time formatting features
旧过时的学期班和 j。 时间班提供格式特征。 无需你写自己的格式编码。
Time Zone
时间区对于确定日期至关重要。 随着东部新时代的到来,这个日期在世界各地因时区而异。 这个问题在问题和其他答复中被忽视。 如果省略,科索沃信托局目前的违约时区就暗中适用。 更明确地规定你所期望/预期的时间区。
LocalDate
如无时日和无时区,则使用。 虽然没有将任何时间区储存在<代码>当地<><>>内,但时间区则确定为“日常”。
ZoneId zoneId = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( zoneId );
ISO 8601
Your desired format of YYYY-MM-DD with double digits happens to comply with the ISO 8601 standard defining sensible formats for textual representations of date-time values. This standard is used by default in java.time classes for parsing/generating strings.
String output = today.toString();
这完全是三条法典。 你们甚至可以把他们结合起来。
String output = LocalDate.now( ZoneId.of( "America/Montreal" ) ).toString();
About java.time
java.time Framework is into Java 8 and subsequently. 这些班级取代麻烦的旧的legacy。 java.util。 日期
<>,rel=“nofollow noretinger”>Calendar
>, &
Joda-time< Project,现在载于mainance Mode,建议向以下人员迁移:a href=“http://docs.ora.com/javase/9/docsa/pi/java/parm/sumer_mage. 班级。
欲了解更多情况,见 rel=“nofollow noreferer”<>>。 此外,还寻找Stack Overflow的许多实例和解释。 具体内容是JSR 310。
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
获取ava。 时间段
>ThreeTen-Extra 项目延长java.time 额外课程。 该项目是今后可能增加 j的一个证明。 时间。 http://www. 33ten.org/ iii/extra/apidocs/org/ 3ten/extra/Interval.html