java joda time - 添加工作日到日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12728527/
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
joda time - add weekdays to date
提问by Bober02
Is it possible to add weekdays to joda time?
是否可以将工作日添加到乔达时间?
For instance, if current date is Friday 01/03, date + 1 should return Monday 04/03, rather than 02/03.
例如,如果当前日期是星期五 01/03,则日期 + 1 应返回星期一 04/03,而不是 02/03。
采纳答案by Jesper
As far as I know there is no built-in method to automatically do this for you in Joda Time. However, you could write your own method, that increments the date in a loop until you get to a weekday.
据我所知,在 Joda Time 中没有内置方法可以自动为您执行此操作。但是,您可以编写自己的方法,在循环中递增日期,直到到达工作日。
Note that, depending on what you need it for exactly, this could be (much) more complicated than you think. For example, should it skip holidays too? Which days are holidays depends on which country you're in. Also, in some countries (for example, Arabic countries) the weekend is on Thursday and Friday, not Saturday and Sunday.
请注意,根据您的具体需要,这可能比您想象的(远)复杂。例如,它也应该跳过假期吗?节假日取决于您所在的国家/地区。此外,在某些国家/地区(例如阿拉伯国家),周末是周四和周五,而不是周六和周日。
回答by vijay
LocalDate newDate = new LocalDate();
int i=0;
while(i<days)//days == as many days as u want too
{
newDate = newDate.plusDays(1);//here even sat and sun are added
//but at the end it goes to the correct week day.
//because i is only increased if it is week day
if(newDate.getDayOfWeek()<=5)
{
i++;
}
}
System.out.println("new date"+newDate);
回答by hendalst
Be aware that iterating through adding N days one at a time can be relatively expensive. For small values of N and/or non performance sensitive code, this is probably not an issue. Where it is, I'd recommend minimizing the add operations by working out how many weeks and days you need to adjust by:
请注意,一次添加 N 天的迭代可能相对昂贵。对于 N 的小值和/或非性能敏感代码,这可能不是问题。在那里,我建议通过计算您需要调整的周数和天数来最小化添加操作:
/**
* Returns the date that is {@code n} weekdays after the specified date.
* <p>
* Weekdays are Monday through Friday.
* <p>
* If {@code date} is a weekend, 1 weekday after is Monday.
*/
public static LocalDate weekdaysAfter(int n, LocalDate date) {
if (n == 0)
return date;
if (n < 0)
return weekdaysBefore(-n, date);
LocalDate newDate = date;
int dow = date.getDayOfWeek();
if (dow >= DateTimeConstants.SATURDAY) {
newDate = date.plusDays(8 - dow);
n--;
}
int nWeeks = n / 5;
int nDays = n % 5;
newDate = newDate.plusWeeks(nWeeks);
return ( (newDate.getDayOfWeek() + nDays) > DateTimeConstants.FRIDAY)
? newDate.plusDays(nDays + 2)
: newDate.plusDays(nDays);
回答by Jana
public LocalDate getBusinessDaysAddedDate(LocalDate localDate, int businessDays){
LocalDate result;
if(localDate.getDayOfWeek().getValue() + businessDays > 5) {
result = localDate.plusDays(2);
}
result = localDate.plusDays(businessDays);
return result;
}
In order to work with Date instead of LocalDate, refer https://stackoverflow.com/a/47719540/12794444for the conversions.
为了使用 Date 而不是 LocalDate,请参阅https://stackoverflow.com/a/47719540/12794444进行转换。
回答by user1581900
Class YearMonthDayis deprecated and you shouldn't use it. If you change to simple DateTime you can obtain the week day by calling:
类YearMonthDay已弃用,您不应使用它。如果您更改为简单的 DateTime,您可以通过调用获取工作日:
dateTime.getDayOfWeek();
For Friday it will be 5.
周五将是 5。
One of the approaches can be making a custom addDays method which should look something like that:
其中一种方法可以是制作一个自定义的 addDays 方法,它应该看起来像这样:
addDays(DateTime dateTime, int days) {
for(int i=0;i<days;i++){
dateTime.plusDays(1);
if(dateTime.getDayOfWeek()==6) dateTime.plusDays(2); // if Saturday add 2 more days }
}