This question already has answers here:
How to subtract X days from a date using Java calendar?
                                
                                    (10个回答)
                                
                        
                4个月前关闭。
            
        

我现在有一个EpochTime,我想获得30天,3个月和1年后的epoch time如何在Java代码中做到这一点。

long todayEpochTime = System.currentTimeMillis()/1000;
System.out.println(todayEpochTime);

最佳答案

您可以使用特定时间Instant处的毫秒数来获取ZoneId的实例,然后减去天,月和年

long todayEpochTime = System.currentTimeMillis();
long dateTime = Instant.ofEpochMilli(todayEpochTime)
                        .atZone(ZoneId.systemDefault())
                        .minusDays(30)
                        .minusMonths(3)
                        .minusYears(1)
                        .toEpochSecond();


    System.out.println(todayEpochTime);
    System.out.println(dateTime);

09-11 19:18
查看更多