英文:
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::minus
从 Instant
中减去周数。由于 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);
英文:
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));
Or, if you are a friend of Optional
s:
Optional.of(timestamp)
.map(Timestamp::toInstant)
.map(t -> t.minus(x * 7L, ChronoUnit.DAYS))
.map(Timestamp::from);
答案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时,将引发以下异常:
异常线程“main”java.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("The time right now (local time): " + Timestamp.from(now));
long numberOfWeeks = 3L;
Instant minusXweeks = now.minus(numberOfWeeks * 7L, ChronoUnit.DAYS);
System.out.println("The time 3 weeks before now (local time): " + 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 "main" 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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论