从当前时间戳减去3周。

huangapple go评论83阅读模式
英文:

Subtract 3 weeks from current timetamp

问题

如何使用java.sql.Timestamp获取当前时间戳减去x周的时间;

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

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

英文:

How can I get the current timestamp - x number of weeks using java.sql.Timestamp;

This is my current timestamp Timestamp.from(Instant.now(clock));

x- could be any number from 0-5

答案1

得分: 4

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

如果不能更改 Instant,我们可以将 Timestamp 转换为 Instant,进行减法操作,然后再转换回来:

Timestamp.from(timestamp.toInstant().minus(x * 7L, ChronoUnit.DAYS));

或者,如果你喜欢使用 Optional

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

<kbd>Ideone 演示</kbd>

英文:

Seeing the code provided, I would suggest to subtract the weeks from the Instant via Instant::minus. Since ChronoUnit.WEEKS is not supported by Instant::minus, we can convert the weeks in days by multiplying them with 7.

If changing the Instant is not an option, we could convert the Timestamp into an Instant, subtract, and convert back:

Timestamp.from(timestamp.toInstant().minus(x * 7L, ChronoUnit.DAYS));

<kbd>Ideone demo</kbd>

Or, if you are a friend of Optionals:

Optional.of(timestamp)
    .map(Timestamp::toInstant)
    .map(t -&gt; t.minus(x * 7L, ChronoUnit.DAYS))
    .map(Timestamp::from);

<kbd>Ideone demo</kbd>

答案2

得分: 2

使用Instant的直接示例使用本地时间

Instant.now(Clock.systemDefaultZone()).minus(numberOfWeeks * 7L, ChronoUnit.DAYS);

上下文中的使用

public static void main(String[] args) {
    Instant now = Instant.now(Clock.systemDefaultZone());
    System.out.println("当前时间(本地时间):" + Timestamp.from(now));

    long numberOfWeeks = 3L;
    Instant minusXweeks = now.minus(numberOfWeeks * 7L, ChronoUnit.DAYS);
    System.out.println("现在之前3周的时间(本地时间):" + Timestamp.from(minusXweeks));
}

输出

当前时间本地时间):2020-08-20 23:24:58.077223
现在之前3周的时间本地时间):2020-07-30 23:24:58.077223

注意

为什么不直接使用ChronoUnit.WEEKS请参见以下内容

Instant.now(Clock.systemDefaultZone()).minus(numberOfWeeks, ChronoUnit.WEEKS)

似乎方法java.time.Instant.minus不支持枚举值ChronoUnit.WEEKS而支持枚举值ChronoUnit.DAYS当在方法java.time.Instant.minus中使用ChronoUnit.WEEKS时将引发以下异常

异常线程mainjava.time.temporal.UnsupportedTemporalTypeException不支持的单位Weeks
at java.base/java.time.Instant.plus(Instant.java:861)
at java.base/java.time.Instant.minus(Instant.java:978)
at TestClass.main(TestClass.java:18)
英文:

Using Instant Directly example, using local time:

Instant.now(Clock.systemDefaultZone()).minus(numberOfWeeks * 7L, ChronoUnit.DAYS);

Used in context:

public static void main(String[] args) {
Instant now = Instant.now(Clock.systemDefaultZone());
System.out.println(&quot;The time right now (local time): &quot; + Timestamp.from(now));
long numberOfWeeks = 3L;
Instant minusXweeks = now.minus(numberOfWeeks * 7L, ChronoUnit.DAYS);
System.out.println(&quot;The time 3 weeks before now (local time): &quot; + Timestamp.from(minusXweeks));
}

Output:

The time right now (local time): 2020-08-20 23:24:58.077223
The time 3 weeks before now (local time): 2020-07-30 23:24:58.077223

NOTE:

Why not use ChronoUnit.WEEKS directly? See below:

Instant.now(Clock.systemDefaultZone()).minus(numberOfWeeks, ChronoUnit.WEEKS)

Seems like ChronoUnit.WEEKS is not supported by method java.time.Instant.minus while enum ChronoUnit.DAYS is. When using ChronoUnit.WEEKS in method java.time.Instant.minus then following exception is thrown:

Exception in thread &quot;main&quot; java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Weeks
at java.base/java.time.Instant.plus(Instant.java:861)
at java.base/java.time.Instant.minus(Instant.java:978)
at TestClass.main(TestClass.java:18)

答案3

得分: 1

从日历的当前时间中减去 x 周,您也可以尝试:

Calendar calendar = Calendar.getInstance();

calendar.add(Calendar.DAY_OF_MONTH, - (7 * no_of_weeks));
英文:

To subtract x weeks from the current time of the calendar, you can also try:

Calendar calendar = Calendar.getInstance();

calendar.add(Calendar.DAY_OF_MONTH, - (7 * no_of_weeks))

huangapple
  • 本文由 发表于 2020年8月21日 04:37:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63512821.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定