计算日期给出两个日期不包括周末
我在Spring 3.0项目中使用乔达时间api来计算日期。现在我有一个开始和结束日期,我希望在这两个日期之间的周末或周六或周日享受每天的免费体验。我怎样才能做到这一点?我看了这个帖子Joda time - all mondays between two dates。 它提供了一些指导,但仍然模糊如何排除两个日期。计算日期给出两个日期不包括周末
我想你的问题是如何
每天除了两个日期间的周末,星期六或星期日获得。
解决方案:
public static void main(String[] args) {
final LocalDate start = LocalDate.now();
final LocalDate end = new LocalDate(2012, 1, 14);
LocalDate weekday = start;
if (start.getDayOfWeek() == DateTimeConstants.SATURDAY ||
start.getDayOfWeek() == DateTimeConstants.SUNDAY) {
weekday = weekday.plusWeeks(1).withDayOfWeek(DateTimeConstants.MONDAY);
}
while (weekday.isBefore(end)) {
System.out.println(weekday);
if (weekday.getDayOfWeek() == DateTimeConstants.FRIDAY)
weekday = weekday.plusDays(3);
else
weekday = weekday.plusDays(1);
}
}
您可以使用公历来检索特定日期的日期。如果字符串是星期六或星期日,您可以忽略它。
我是新来的。 我一直在寻找解决这个问题,并没有使用循环,但没有找到合适的算法。所以我决定创建这个不使用循环的解决方案,效率很高,代码已经过测试。
public int betweenDaysIgnoreWeekends(DateTime startDate, DateTime endDate) {
//Um numero que representa o dia da semana para a data final, exemplo segunda=1, terça=2, quarta=3...
int dayOfWeekEndDateNumber = Integer.valueOf(endDate.dayOfWeek()
.getAsString());
//Um numero que representa o dia da semana para a data inicial, exemplo segunda=1, terça=2, quarta=3...
int dayOfWeekStartDateNumber = Integer.valueOf(startDate.dayOfWeek()
.getAsString());
//Se a data final for sabado ou domingo, finja ser sexta-feira
if (dayOfWeekEndDateNumber == 6 || dayOfWeekEndDateNumber == 7) {
int DaysToAdd = 8 - dayOfWeekEndDateNumber;
endDate = endDate.plusDays(DaysToAdd);
dayOfWeekEndDateNumber = Integer.valueOf(endDate.dayOfWeek()
.getAsString());
}
//Se a data inicial for sabado ou domingo, finja ser segunda-feira
if (dayOfWeekStartDateNumber == 6 || dayOfWeekStartDateNumber == 7) {
int DaysToAdd = 8 - dayOfWeekStartDateNumber;
startDate = startDate.plusDays(DaysToAdd);
dayOfWeekStartDateNumber = Integer.valueOf(startDate.dayOfWeek()
.getAsString());
}
//Quantos dias se passaram contando os fins de semana
int days = Days.daysBetween(startDate, endDate).getDays();
//Quantas semanas se passaram exatamente
int weeks = days/7;
//O excesso de dias que sobrou, exemplo: 1 semana e 3 dias o excess=3 e weeks=1
int excess = days % 7;
//Se a data inicial for igual a data final, passou 0 dia
if (startDate.equals(endDate)) {
return 0;
} else {
//O excesso de dias passou pelo fim de semana, então deve-se retirar 2 dias
//da quantidade final de dias
if (excess + dayOfWeekStartDateNumber >= 6) {
//Quantidade de semanas * 5 dias uteis + o excesso de dias - o final de semana que o excesso atravessou
return weeks * 5 + excess - 2;
}
//Quantidade de semanas * 5 dias uteis + o excesso de dias
return weeks * 5 + excess;
}
}
为了提高在萨米尔 - 马查多 - 德 - 奥利维拉贴什么@,这里是将计算天SANS周六日不使用循环的功能。我没有基准这对循环版本,但它看起来好像它会更快:
/**
* Gets number of days between two dates. Ignoring weekends.
* @param startDate
* @param endDate
* @return
*/
public static int getDaysBetweenIgnoreWeekends(DateTime startDate, DateTime endDate) {
// If the start date is equal to the closing date, spent 0 days
if (startDate.equals(endDate))
return 0;
// A number that represents the day for the start date, Monday = 1 , Tuesday = 2 , Wednesday = 3 ...
int dayOfWeekStartDateNumber = startDate.getDayOfWeek();
// If the starting date is Saturday or Sunday , pretend to be Monday
if (dayOfWeekStartDateNumber == 6 || dayOfWeekStartDateNumber == 7) {
int DaysToAdd = 8 - dayOfWeekStartDateNumber;
startDate = startDate.plusDays(DaysToAdd);
dayOfWeekStartDateNumber = Integer.valueOf(startDate.dayOfWeek().getAsString());
}
// How many days have passed counting weekends
int days = Days.daysBetween(startDate, endDate).getDays();
// How many weeks have passed
int weeks = days/7;
// Excess days left. E.g. one week and three days the excess will be 3
int excess = days % 7;
// Excess of days spent for the weekend , then it must be removed two days
// the final number of days
if (excess + dayOfWeekStartDateNumber >= 6) {
// Week count * 5 working days + excess days - the weekend that excess crossed
return weeks * 5 + excess - 2;
}
// Weeks count * 5 working days + excess days
return weeks * 5 + excess;
}
而且,这里是一个版本,让你忽略了一天的时间,因此,如果开始日期是在开始时间的上午11点,结束时间是在上午10点的第二天,它将显示为1天而不是0天。
/**
* Gets number of days between two dates. Ignoring weekends. Ignores Hours.
* @param startDate
* @param endDate
* @return
*/
public static int getDaysBetweenIgnoreWeekends(DateTime startDate, DateTime endDate) {
return getDaysBetweenIgnoreWeekends(startDate,endDate,true);
}
/**
* Gets number of days between two dates. Ignoring weekends.
* @param startDate
* @param endDate
* @param ignoreTimeOfDay
* @return
*/
public static int getDaysBetweenIgnoreWeekends(DateTime startDate, DateTime endDate, boolean ignoreTimeOfDay) {
// If the start date is equal to the closing date, spent 0 days
if (startDate.equals(endDate))
return 0;
if (ignoreTimeOfDay && startDate.toLocalDate().equals(endDate.toLocalDate()))
return 0;
// A number that represents the day for the start date, Monday = 1 , Tuesday = 2 , Wednesday = 3 ...
int dayOfWeekStartDateNumber = startDate.getDayOfWeek();
// If the starting date is Saturday or Sunday , pretend to be Monday
if (dayOfWeekStartDateNumber == 6 || dayOfWeekStartDateNumber == 7) {
int DaysToAdd = 8 - dayOfWeekStartDateNumber;
startDate = startDate.plusDays(DaysToAdd);
dayOfWeekStartDateNumber = Integer.valueOf(startDate.dayOfWeek().getAsString());
}
// How many days have passed counting weekends
int days;
if(ignoreTimeOfDay) {
days = Days.daysBetween(startDate.toLocalDate(), endDate.toLocalDate()).getDays();
} else {
days = Days.daysBetween(startDate, endDate).getDays();
}
// How many weeks have passed
int weeks = days/7;
// Excess days left. E.g. one week and three days the excess will be 3
int excess = days % 7;
// Excess of days spent for the weekend , then it must be removed two days
// the final number of days
if (excess + dayOfWeekStartDateNumber >= 6) {
// Week count * 5 working days + excess days - the weekend that excess crossed
return weeks * 5 + excess - 2;
}
// Weeks count * 5 working days + excess days
return weeks * 5 + excess;
}
这将不包括周末返回结束日期
public static Date addDaysBySkipWeekend(Date startDate, int numDays) {
Calendar dateCal = Calendar.getInstance();
dateCal.setTime(startDate);
for (int i = 0; i < numDays-1; i++) {
dateCal.add(dateCal.DATE, 1);
if(dateCal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
|| dateCal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ){
dateCal.add(dateCal.DATE, 1);
i--;
}
}
return dateCal.getTime();
}
我一直在使用@Josh马格的逻辑将近一年了,但最近发现它时,结束日期恰好落在返回错误值星期六。
以下是我想要分享的版本,当它碰巧是星期六或星期天时,它会考虑从endDate中减去天数。
public static int getDaysBetweenIgnoreWeekends(org.joda.time.DateTime startDate, org.joda.time.DateTime endDate, boolean ignoreTimeOfDay) {
// If the start date is equal to the closing date, spent 0 days
if (startDate.equals(endDate))
return 0;
if (ignoreTimeOfDay && startDate.toLocalDate().equals(endDate.toLocalDate()))
return 0;
// A number that represents the day for the start date, Monday = 1 , Tuesday = 2 , Wednesday = 3 ...
int dayOfWeekStartDateNumber = startDate.getDayOfWeek();
int dayOfWeekEndDateNumber = endDate.getDayOfWeek();
// If the starting date is Saturday or Sunday , pretend to be Monday
if (dayOfWeekStartDateNumber == 6 || dayOfWeekStartDateNumber == 7) {
int DaysToAdd = 8 - dayOfWeekStartDateNumber;
startDate = startDate.plusDays(DaysToAdd);
dayOfWeekStartDateNumber = Integer.valueOf(startDate.dayOfWeek().getAsString());
}
org.joda.time.DateTime effectiveEndDate = endDate;
if (dayOfWeekEndDateNumber == 6 || dayOfWeekEndDateNumber == 7) {
effectiveEndDate = endDate.minusDays(Math.abs(5 - dayOfWeekEndDateNumber));
}
// How many days have passed counting weekends
int days;
if(ignoreTimeOfDay) {
days = org.joda.time.Days.daysBetween(startDate.toLocalDate(), effectiveEndDate.toLocalDate()).getDays();
} else {
days = org.joda.time.Days.daysBetween(startDate, effectiveEndDate).getDays();
}
// How many weeks have passed
int weeks = days/7;
// Excess days left. E.g. one week and three days the excess will be 3
int excess = days % 7;
// Excess of days spent for the weekend , then it must be removed two days
// the final number of days
if (excess + dayOfWeekStartDateNumber >= 6) {
// Week count * 5 working days + excess days - the weekend that excess crossed
return weeks * 5 + excess - 2;
}
// Weeks count * 5 working days + excess days
return weeks * 5 + excess;
}
在早期版本 - 下面的代码片段是减去额外的一天。无论endDate是星期六还是星期天,都减去-2。
if (excess + dayOfWeekStartDateNumber >= 6) {
// Week count * 5 working days + excess days - the weekend that excess crossed
return weeks * 5 + excess - 2;
}
希望帮助!
它像一个魅力一样工作。万分感谢! :) – shanti
'计算天数'或'得到所有日期'? – lschin