java.util.Date - 从日期中删除三个月?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1311143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.util.Date - Deleting three months from a date?
提问by AJM
I have a date of type java.util.Date
我有一个日期类型 java.util.Date
I want to subtract three months from it.
我想从中减去三个月。
Not finding a lot of joy in the API.
在 API 中没有找到很多乐趣。
采纳答案by Daniel Rikowski
Here's the plain JDKversion, it needs the Calendar
class as a helper:
这是普通的JDK版本,它需要Calendar
该类作为助手:
Date referenceDate = new Date();
Calendar c = Calendar.getInstance();
c.setTime(referenceDate);
c.add(Calendar.MONTH, -3);
return c.getTime();
But you should seriously consider using the Joda library, because of various shortcomings of the Date
and Calendar
classes. With Joda you can do the following:
但是你应该认真考虑使用Joda 库,因为Date
和Calendar
类的各种缺点。使用 Joda,您可以执行以下操作:
new DateTime().minusMonths(3).toDate();
Or if you want to subtract from a given date instead of the current:
或者,如果您想从给定日期而不是当前日期中减去:
new DateTime(referenceDate).minusMonths(3).toDate();
Update for Java 8:With Java 8 you can also use the new JSR 310 API (which is inspired by Joda):
Java 8 更新:使用 Java 8,您还可以使用新的 JSR 310 API(受 Joda 启发):
LocalDateTime.from(referenceDate.toInstant()).minusMonths(3);
回答by firstthumb
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.MONTH, -3);
Set your date using setTime
method.
使用setTime
方法设置您的日期。
回答by Everyone
The Date class itself isn't enough (+: You've got to use the Calendar class here
Date 类本身是不够的(+:你必须在这里使用 Calendar 类
Something along these lines
沿着这些路线的东西
GregorianCalendar lCalendar = new GregorianCalendar();
lCalendar.setTime( aDate );
lCalendar.add(Calendar.MONTH, -3);
p.s. the snippet above is not tested to be compilable.
ps 上面的代码片段未经测试可编译。
回答by Brian Agnew
I alwaysrecommend Jodafor this sort of stuff. It has a much nicer API, and doesn't suffer from threading issues that the standard Java date/time has (e.g. issues with SimpleDateFormat
, or general mutability).
我总是推荐Joda做这类事情。它有一个更好的 API,并且不会遇到标准 Java 日期/时间所具有的线程问题(例如,与SimpleDateFormat
或一般可变性有关的问题)。
e.g.
例如
DateTime result = dt.minusMonths(3);
回答by TheWestIsThe...
public static Date getDateMonthsAgo(int numOfMonthsAgo)
{
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.MONTH, -1 * numOfMonthsAgo);
return c.getTime();
}
will return the date X months in the past. Similarily, here's a function that returns the date X days in the past.
将返回过去 X 个月的日期。类似地,这里有一个函数返回过去 X 天的日期。
public static Date getDateDaysAgo(int numOfDaysAgo)
{
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.DAY_OF_YEAR, -1 * numOfDaysAgo);
return c.getTime();
}
回答by Valentin Montmirail
You want today - 3 Month formatted as dd MMMM yyyy
你想要今天 - 3 个月格式为 dd MMMM yyyy
SimpleDateFormat format = new SimpleDateFormat("dd MMMM yyyy");
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.add(Calendar.MONTH, -3);
Date d = c.getTime();
String res = format.format(d);
System.out.println(res);
So this code can do the job ;)
所以这段代码可以完成这项工作;)
回答by Patrik Bego
Ok with java.sql.Date (subclass of java.util.Date) and JDK's 8 LocalDate help you can do it in one line ;)
好的 java.sql.Date(java.util.Date 的子类)和 JDK 的 8 LocalDate 帮助您可以在一行中完成;)
Date date = java.sql.Date.valueOf(LocalDate.now().minus(3, ChronoUnit.MONTHS));
回答by Robert Niestroj
You can use Apache Commons Lang3DateUtilsaddMonthsfunction.
您可以使用Apache Commons Lang3 DateUtils addMonths函数。
static Date addMonths(Date date, int amount)
Adds a number of months to a date returning a new object. The original Date is unchanged. The amount to add, may be negative, so you can go 3 months back.
将月份数添加到返回新对象的日期。原始日期不变。添加的金额可能是负数,因此您可以返回 3 个月。
回答by Gelberth Amarillo Rojas
You can use
您可以使用
Date d1 = new Date()
d1.setMonth(d1.month-3)
Hope this helps
希望这可以帮助
回答by George Siggouroglou
Using Java 8you can do it like this,
使用Java 8你可以这样做,
Date d = Date.from(LocalDate.now().minusMonths(3).atStartOfDay(ZoneId.systemDefault()).toInstant());
The LocalDate
class has a lot of methods to help you make easy computations about dates like the above,
该LocalDate
班有很多的方法来帮助你做出像上面的日期很容易计算,
// Add 2 months
Date d = Date.from(LocalDate.now().plusMonths(2).atStartOfDay(ZoneId.systemDefault()).toInstant());
// Add 5 days
Date d = Date.from(LocalDate.now().plusDays(5).atStartOfDay(ZoneId.systemDefault()).toInstant());
// Minus 1 day and 1 year
Date d = Date.from(LocalDate.now().minusYears(1).minusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant());
In order to compute time you can use the LocalDateTime
class,
为了计算时间,您可以使用LocalDateTime
该类,
// Minus 1 year, minus 1 days, plus 1 hour
Date d = Date.from(LocalDateTime.now().minusYears(1).minusDays(1).plusHours(1).toLocalDate().atStartOfDay(ZoneId.systemDefault()).toInstant());