如何使用 Java 日历从日期中减去 X 天? [英] How to subtract X days from a date using Java calendar?

查看:30
本文介绍了如何使用 Java 日历从日期中减去 X 天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道使用 Java 日历从日期中减去 X 天的简单方法吗?

Anyone know a simple way using Java calendar to subtract X days from a date?

我找不到任何允许我直接从 Java 中的日期减去 X 天的函数.有人能指出我正确的方向吗?

I have not been able to find any function which allows me to directly subtract X days from a date in Java. Can someone point me to the right direction?

推荐答案

摘自 此处的文档:

根据日历的规则向给定的日历字段添加或减去指定的时间量.例如,从日历的当前时间减去5天,可以通过调用来实现:

Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules. For example, to subtract 5 days from the current time of the calendar, you can achieve it by calling:

Calendar calendar = Calendar.getInstance(); // this would default to now
calendar.add(Calendar.DAY_OF_MONTH, -5).

这篇关于如何使用 Java 日历从日期中减去 X 天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆