Using .from() to instantiate LocalDate, LocalTime, YearMonth, or Year from a
current time expression is unnecessarily verbose when a direct .now() factory method is available.
Some .from( ) patterns are unnecessarily verbose and can be simplified using the now( ) method.
For example LocalDate.from(Instant.now().atZone(zone)) can be replaced with LocalDate.now(zone)
Calling .now() directly on the target type is more concise, and makes the intent clearer.
Call .now(), .now(ZoneId), or .now(Clock) directly on the target type instead of using .from()
with a current time expression.
import java.time.*;
void execute(Instant instant, ZoneId zoneId, Clock clock) {
LocalDate date = LocalDate.from(Instant.now().atZone(ZoneId.of("UTC"))); // Noncompliant
LocalTime time = LocalTime.from(LocalTime.now(zoneId)); // Noncompliant
YearMonth yearMonth = YearMonth.from(ZonedDateTime.now(clock)); // Noncompliant
// ...
}
import java.time.*;
void execute(Instant instant, ZoneId zoneId, Clock clock) {
LocalDate date = LocalDate.now(ZoneId.of("UTC"));
LocalTime time = LocalTime.now(zoneId);
YearMonth yearMonth = YearMonth.now(clock);
// ...
}