
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
44 lines
1.1 KiB
Plaintext
44 lines
1.1 KiB
Plaintext
== Why is this an issue?
|
|
|
|
The use of the ``++ZonedDateTime++`` class introduced in Java 8 to truncate a date can be significantly faster than the ``++DateUtils++`` class from Commons Lang.
|
|
|
|
|
|
*Note* that this rule is automatically disabled when the project's ``++sonar.java.source++`` is lower than ``++8++``.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
public Date trunc(Date date) {
|
|
return DateUtils.truncate(date, Calendar.SECOND); // Noncompliant
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
public Date trunc(Date date) {
|
|
Instant instant = date.toInstant();
|
|
ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
|
|
ZonedDateTime truncatedZonedDateTime = zonedDateTime.truncatedTo(ChronoUnit.SECONDS);
|
|
Instant truncatedInstant = truncatedZonedDateTime.toInstant();
|
|
return Date.from(truncatedInstant);
|
|
}
|
|
----
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Use "ZonedDateTime.truncatedTo" instead. [(sonar.java.source not set. Assuming 8 or greater.)]
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|