Tagged: optional

changing a nullable milliseconds to a nullable date using optional.


public Date toDate(@Nullable Long millis) {
    //return millis == null ? null : new Date(millis);
    return Optional.ofNullable(millis).map(Date::new).orElse(null);
}

Returning an Optional rather than a nullable Date instance is a better idea.

public Optional<Date> toDate(@Nullable Long millis) {
    return Optional.ofNullable(millis).map(Date::new);
}