从当前时间戳减去 3 周

Nee*_*tha -1 java timestamp sql-timestamp

如何使用 java.sql.Timestamp; 获取当前时间戳 - x 周数;

这是我当前的时间戳Timestamp.from(Instant.now(clock));

x- 可以是 0-5 之间的任意数字

Tur*_*g85 6

看到提供的代码,我建议从Instantvia中减去周数Instant::minus。由于ChronoUnit.WEEKS不支持Instant::minus,我们可以通过将周乘以 7 来转换为天。

如果Instant无法更改 ,我们可以将 转换TimestampInstant,减去,然后再转换回来:

Timestamp.from(timestamp.toInstant().minus(x * 7L, ChronoUnit.DAYS));
Run Code Online (Sandbox Code Playgroud)

Ideone demo

或者,如果您是 s 的朋友Optional

Optional.of(timestamp)
    .map(Timestamp::toInstant)
    .map(t -> t.minus(x * 7L, ChronoUnit.DAYS))
    .map(Timestamp::from);
Run Code Online (Sandbox Code Playgroud)

Ideone demo