gpt4 book ai didi

java - Simpledateformat - 为什么 Calendar.MONTH 错误地生成 16 年 1 月的上个月值(即 Dec-2015)?

转载 作者:搜寻专家 更新时间:2023-11-01 03:00:45 25 4
gpt4 key购买 nike

我一直在尝试使用 Simpledateformat 从当前月份获取过去 12 个月的值。但是对于上个月(即 2015 年 12 月),我总是得到 2016 年 12 月。

calendar.set(Calendar.MONTH, -1);这应该会在 2015 年 12 月返回,但实际上并没有。

我不明白这个逻辑。有人可以解释一下吗?非常感谢您的时间和帮助。

我的代码:

public static void main (String[] args) throws java.lang.Exception
{
SimpleDateFormat month_date = new SimpleDateFormat("MMM-YYYY");

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, -1);


String month_name = month_date.format(calendar.getTime());
System.out.println("month_name : "+month_name);

}

输出:

month_name : Dec-2016

最佳答案

要减去月份,您可以这样做:

calendar.add(Calendar.MONTH, -1);

希望对你有帮助。

关于java - Simpledateformat - 为什么 Calendar.MONTH 错误地生成 16 年 1 月的上个月值(即 Dec-2015)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35083824/

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