【问题标题】:add seconds to System.currentTimeMills()将秒数添加到 System.currentTimeMillis()
【发布时间】:2021-01-03 19:06:46
【问题描述】:

我正在尝试使用以下方法计算 100 天后的时间:

import java.util.Date;
public class Main
{
    public static void main(String[] args) {
        System.out.println("Hello World");
        System.out.println(new Date(System.currentTimeMillis() + 8640000 * 1000));
    }
}

它给了我Jan 04 08:57:25 UTC 2021 这是不正确的。我该如何补救?

【问题讨论】:

  • 整数溢出。 8640000×1000 > 2³¹。将 8640000 更改为 8640000L,以便 Java 知道您正在使用 64 位 long 值进行数学运算,这不会溢出。
  • 也可以考虑LocalDateTime.now().plus(Duration.ofDays(100))
  • @trashgod 为什么plus(Duration.ofDays(100)) plusDays(100) 可用?
  • 虽然在 UTC 中 100 天等于您尝试计算的毫秒数,但在许多其他时区通常情况并非如此,因为一天可能是 23 或 25 小时。
  • @Andreas:好问题;我开始走那条路,但我想强调Duration 的概念。

标签: java


【解决方案1】:

使用以下内容:

System.out.println(new Date(System.currentTimeMillis() + (long)8640000 * 1000));

或者

System.out.println(new Date(System.currentTimeMillis() + 8640000L * 1000));

8640000 * 1000 是 8,640,000,000,超过了 Integer.MAX_VALUE 的 2,147,483,647。您可以通过将其转换为 long 来解决此问题。

这给出了结果:

Tue Apr 13 15:26:10 EDT 2021

【讨论】:

    【解决方案2】:

    java.util 的日期时间 API 及其格式化 API SimpleDateFormat 已过时且容易出错。建议完全停止使用,转用modern date-time API

    使用现代 API:

    import java.time.LocalDate;
    import java.time.ZoneId;
    
    public class Main {
        public static void main(String[] args) {
            // Change the ZoneId as per your requirement e.g. ZoneId.of("Europe/London")
            LocalDate today = LocalDate.now(ZoneId.systemDefault());
            LocalDate after100Days = today.plusDays(100);
            System.out.println(after100Days);
        }
    }
    

    输出:

    2021-04-13
    

    Trail: Date Time 了解现代日期时间 API。

    使用旧版 API:

    import java.util.Calendar;
    import java.util.Date;
    import java.util.TimeZone;
    
    public class Main {
        public static void main(String[] args) {
            // Change the TimeZone as per your requirement e.g. TimeZone.getTimeZone("Europe/London")
            Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
            calendar.add(Calendar.DAY_OF_YEAR, 100);
            Date after100Days = calendar.getTime();
            System.out.println(after100Days);
        }
    }
    

    输出:

    Tue Apr 13 19:40:11 BST 2021
    

    【讨论】:

      【解决方案3】:

      有时间试试

      LocalDateTime.now().plusDays(100).toString()
      

      SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z"); // you could used any format
      Calendar c = Calendar.getInstance();
      c.add(Calendar.DATE, 100); // Adding 100 days
      String output = sdf.format(c.getTime());
      System.out.println(output);
      

      【讨论】:

      • c.setTime(new Date()); 是多余的,因为getInstance() 返回的Calendar 对象已经是当前时间:"获取使用默认时区和语言环境的日历。返回的日历基于默认时区的当前时间,使用默认的 FORMAT 语言环境。"
      • 是的,你是对的,只是添加它以防你想重置日期。我已经编辑了它
      猜你喜欢
      • 2010-12-23
      • 2013-02-12
      • 2011-05-31
      • 2013-07-01
      • 2017-03-16
      • 1970-01-01
      • 2015-08-13
      • 2017-09-15
      • 1970-01-01
      相关资源
      最近更新 更多