gpt4 book ai didi

java Calendar/format 提供错误的日期

转载 作者:行者123 更新时间:2023-12-02 08:22:27 26 4
gpt4 key购买 nike

不知何故,我收到了一个问题报告,其中以下代码提供了 future 的日期。

使用的时区是 GMT+01:00。numberOfDays 是非负整数。

此代码的目的是减少当前日期的天数。

SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yy",Locale.ENGLISH);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -numberOfDays);
Date date = calendar.getTime();
String dateStr= formatter.format(date);
System.out.println("Date : "+dateStr);

我无法在我的机器上重现此内容。

区域设置会影响时区吗?

我尝试关联到 Why does a new SimpleDateFormat object contain calendar with the wrong year? ,和 Strange problem with timezone, calendar and SimpleDateFormat但徒劳。

请帮助我理解并纠正此问题。

最佳答案

嗯,我立即想到了两种可能性:

  • 客户端计算机上的系统日期不正确,因此日历以 future 的日期开始
  • 如果 numberOfDays 为负数,显然会将日期推到 future

区域设置与时区没有直接关系 - 它们是独立的,尽管显然具有法国区域设置的计算机可能处于法国时间区域等

就我个人而言,我会完全避免使用日期/日历并使用 Joda Time作为一个更好的日期和时间 API,但这对我上面给出的任何一个想法都没有帮助......

关于java Calendar/format 提供错误的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5219201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com