在日历对象中减去天数[重复]

时间:2022-08-25 18:08:39

Possible Duplicate:
Anyone know a simple way using java calendar to subtract X days to a date?

可能的重复:有人知道用java日历减去一个日期的X天的简单方法吗?

I need to minus 365 days in a given date (givenDate)-

我需要在给定的日期(givenDate)减去365天。

Calendar calendar = Calendar.getInstance();
calendar.setTime(givenDate);
calendar.add(Calendar.DATE, -365);  

Am I right?

我说的对吗?

5 个解决方案

#1


13  

Calendar.DAY_OF_YEAR is the proper way to subtract days

日历。年日是减去天数的正确方法

You can also subtract a year (taking in to account leap years) by using

你也可以用减去一年(考虑到闰年)

Calendar calendar = Calendar.getInstance();
calendar.setTime(givenDate);
calendar.add(Calendar.YEAR, -1);

#2


4  

That is the correct way to subtract days.

这是减去天数的正确方法。

Note that 365 days does not always equal one year because of leap days. calendar.add(Calendar.YEAR, -1) would subtract one year correctly.

注意,由于闰日,365天并不总是等于一年。calendar.add(日历。年,-1)将正确地减去一年。

You also may want to use Joda Time-library instead of java.util.Date and java.util.Calendar. Joda Time is a much nicer API for handling times and dates.

您还可能希望使用Joda时间库,而不是java.util。日期和java.util.Calendar。Joda Time是一个更好的API,用于处理时间和日期。

#3


1  

I don't think it'll make a different, but I would use Calendar.DAY_OF_YEAR as the field.

我不认为会有什么不同,但我会用日历。DAY_OF_YEAR字段。

#4


0  

If you are trying to strictly subtract 365 days, then yeah, that'd do it. However, if you are trying years backward, that might not work due to leap years.

如果你要严格地减去365天,那就可以了。然而,如果你正在倒退几年,这可能不会起作用,因为闰年。

#5


0  

Check out Veyder-time. It is a simple and powerful alternativ to java.util.Calendar and has simple methods for adding and subtracting both days and years, among many other things.

查看Veyder-time。对于java.util来说,这是一个简单而强大的替代。日历,有简单的方法添加和减去天和年,以及许多其他东西。

#1


13  

Calendar.DAY_OF_YEAR is the proper way to subtract days

日历。年日是减去天数的正确方法

You can also subtract a year (taking in to account leap years) by using

你也可以用减去一年(考虑到闰年)

Calendar calendar = Calendar.getInstance();
calendar.setTime(givenDate);
calendar.add(Calendar.YEAR, -1);

#2


4  

That is the correct way to subtract days.

这是减去天数的正确方法。

Note that 365 days does not always equal one year because of leap days. calendar.add(Calendar.YEAR, -1) would subtract one year correctly.

注意,由于闰日,365天并不总是等于一年。calendar.add(日历。年,-1)将正确地减去一年。

You also may want to use Joda Time-library instead of java.util.Date and java.util.Calendar. Joda Time is a much nicer API for handling times and dates.

您还可能希望使用Joda时间库,而不是java.util。日期和java.util.Calendar。Joda Time是一个更好的API,用于处理时间和日期。

#3


1  

I don't think it'll make a different, but I would use Calendar.DAY_OF_YEAR as the field.

我不认为会有什么不同,但我会用日历。DAY_OF_YEAR字段。

#4


0  

If you are trying to strictly subtract 365 days, then yeah, that'd do it. However, if you are trying years backward, that might not work due to leap years.

如果你要严格地减去365天,那就可以了。然而,如果你正在倒退几年,这可能不会起作用,因为闰年。

#5


0  

Check out Veyder-time. It is a simple and powerful alternativ to java.util.Calendar and has simple methods for adding and subtracting both days and years, among many other things.

查看Veyder-time。对于java.util来说,这是一个简单而强大的替代。日历,有简单的方法添加和减去天和年,以及许多其他东西。