Instant获取30天以前的时间

30天前,现在是2020.5.14

传入天单位:

 

Instant requestTimeUtc = Instant.now().minusMillis(TimeUnit.DAYS.toMillis(30));

传入秒单位 

Instant requestTimeUtc = Instant.now().minusSeconds(2592000);

 

demo:

public static void main(String[] args) {

		Instant day = Instant.now().minusMillis(TimeUnit.DAYS.toMillis(30));
		Instant s = Instant.now().minusSeconds(2592000);
		System.out.println(day);
		System.out.println(s);
	}

输出:

2020-04-14T10:45:46.753Z
2020-04-14T10:45:46.753Z


Process finished with exit code 0

还可以扩展年、月、周、时等,可以查看

java.util.concurrent.TimeUnit

 

ps:博客纯属个人日常笔记收集

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Java的`java.time`包中的`LocalDateTime`和`Instant`类来获取30天前的时间戳。具体代码如下: ```java import java.time.LocalDateTime; import java.time.Instant; import java.time.temporal.ChronoUnit; public class Example { public static void main(String[] args) { // 获取当前时间 LocalDateTime currentDateTime = LocalDateTime.now(); // 获取30天前的时间 LocalDateTime beforeDateTime = currentDateTime.minus(30, ChronoUnit.DAYS); // 转换为时间Instant beforeInstant = beforeDateTime.toInstant(ZoneOffset.UTC); long beforeTimestamp = beforeInstant.toEpochMilli(); System.out.println("30天前的时间戳:" + beforeTimestamp); } } ``` 上述代码中,我们首先获取当前时间`currentDateTime`,然后使用`minus()`方法获取30天前的时间`beforeDateTime`,接着使用`toInstant()`方法将其转换为`Instant`类型的时间戳,最后使用`toEpochMilli()`方法获取毫秒级时间戳。 ### 回答2: 在Java中获取30天前的时间戳可以通过以下步骤实现。 首先,可以使用`java.time`包中的`LocalDateTime`类来表示当前时间。我们可以使用`LocalDateTime.now()`方法来获取当前的日期和时间。 接下来,我们可以使用`minusDays()`方法来减去30天。具体步骤如下: ```java import java.time.LocalDateTime; public class Main { public static void main(String[] args) { LocalDateTime currentDateTime = LocalDateTime.now(); // 获取当前时间 LocalDateTime thirtyDaysAgo = currentDateTime.minusDays(30); // 减去30天 long timestamp = thirtyDaysAgo.toEpochSecond(java.time.OffsetDateTime.now().getOffset()); // 将时间转换为时间戳 System.out.println(timestamp); // 输出时间戳 } } ``` 在上述代码中,我们首先获取当前时间并存储在`currentDateTime`变量中。然后使用`minusDays(30)`方法来减去30天,并将结果存储在`thirtyDaysAgo`变量中。最后,我们使用`toEpochSecond()`方法将`thirtyDaysAgo`转换为时间戳,并存储在`timestamp`变量中。 最后,我们可以使用`System.out.println()`来打印出获取到的时间戳。 运行上述代码,将会输出30天前的时间戳。 ### 回答3: Java中获取30天前的时间戳可以使用以下代码: ```java import java.time.LocalDate; import java.time.ZoneOffset; public class Main { public static void main(String[] args) { // 获取当前日期 LocalDate currentDate = LocalDate.now(); // 获取30天前的日期 LocalDate thirtyDaysBefore = currentDate.minusDays(30); // 将日期转换为时间戳(以秒为单位) long timestamp = thirtyDaysBefore.atStartOfDay().toEpochSecond(ZoneOffset.UTC); System.out.println(timestamp); } } ``` 首先,我们使用`LocalDate.now()`获取当前的日期。然后,使用`minusDays(30)`方法从当前日期减去30天,得到30天前的日期。接下来,我们将这个日期转换为时间戳,这里使用的是`atStartOfDay().toEpochSecound(ZoneOffset.UTC)`方法,将日期转换为以UTC时区为基准的秒数的时间戳。最后,我们将时间戳打印出来。 以上代码的输出将是30天前的时间戳。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值