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.

Why is this an issue?

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.

How to fix it

Call .now(), .now(ZoneId), or .now(Clock) directly on the target type instead of using .from() with a current time expression.

Code examples

Noncompliant code example

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
    // ...
  }

Compliant solution

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);
    // ...
  }

Resources

Documentation