java根据天数计算周数和剩余的天数_使用Java 8计算两个日期之间的天数,同时忽略一周中的某些天...

@H_404_1@下面我有3种方法.第一个很简单.它只计算总天数.但是,第二个不仅会计算天数,还会忽略传递给方法的星期几.

我的问题是第三种方法并不总是正确的.它应该匹配第二种方法.我猜它与闰年有关,因为当它不正确时,差异通常是= 3 | 4.

附加信息

我试图以某种方式模拟Excel的工作日(serial_number,[return_type])公式.

serial_number = startDate:Date - daysOfWeekToInclude:Array

| A | B | C

+---------+----------------------------------------------------+-----------

1 | Start | =DATE(2014,9,7) | 9/7/2014

2 | End | =DATE(2025,6,13) | 6/13/2025

3 | Include | ={1,2,4,6} (Mon,Tue,Thu,& Sat) |

4 | Days | =SUM(INT((WEEKDAY($B$1-{1,6},1)+$B$2-$B$1)/7)) | 2248

原始图像

>只需计算两个日期之间的天数.

public static int simpleDaysBetween(final LocalDate start,final LocalDate end) {

return (int) ChronoUnit.DAYS.between(start,end);

}

>使用循环计算天数,忽略一周中的某些天.

public static int betterDaysBetween(final LocalDate start,final LocalDate end,final List ignore) {

int count = 0;

LocalDate curr = start.plusDays(0);

while (curr.isBefore(end)) {

if (!ignore.contains(curr.getDayOfWeek())) {

count++;

}

curr = curr.plusDays(1); // Increment by a day.

}

return count;

}

>计算天数.再次,但没有循环.

public static int bestDaysBetween(final LocalDate start,final List ignore) {

int days = simpleDaysBetween(start,end);

if (days == 0) {

return 0;

}

if (!ignore.isEmpty()) {

int weeks = days / 7;

int startDay = start.getDayOfWeek().getValue();

int endDay = end.getDayOfWeek().getValue();

int diff = weeks * ignore.size();

for (DayOfWeek day : ignore) {

int currDay = day.getValue();

if (startDay <= currDay) {

diff++;

}

if (endDay > currDay) {

diff++;

}

}

if (endDay > startDay) {

diff -= endDay - startDay;

}

return days - diff;

}

return days;

}

完整代码

import java.time.DayOfWeek;

import java.time.LocalDate;

import java.time.temporal.ChronoUnit;

import java.util.Arrays;

import java.util.List;

public class DayCounter {

public static void main(String[] args) {

final LocalDate start = LocalDate.of(2014,7);

final LocalDate end = LocalDate.of(2025,13);

List ignore = Arrays.asList(DayOfWeek.SUNDAY,DayOfWeek.WEDNESDAY,DayOfWeek.FRIDAY);

print(start);

print(end);

System.out.println(simpleDaysBetween(start,end));

System.out.println(betterDaysBetween(start,end,ignore));

System.out.println(bestDaysBetween(start,ignore));

}

public static void print(LocalDate date) {

System.out.printf("%s -> %s%n",date,date.getDayOfWeek());

}

public static int simpleDaysBetween(final LocalDate start,final LocalDate end) {

return (int) ChronoUnit.DAYS.between(start,end);

}

public static int betterDaysBetween(final LocalDate start,final List ignore) {

int count = 0;

LocalDate curr = start.plusDays(0);

while (curr.isBefore(end)) {

if (!ignore.contains(curr.getDayOfWeek())) {

count++;

}

curr = curr.plusDays(1); // Increment by a day.

}

return count;

}

public static int bestDaysBetween(final LocalDate start,final List ignore) {

int days = simpleDaysBetween(start,end);

if (days == 0) {

return 0;

}

if (!ignore.isEmpty()) {

int weeks = days / 7;

int startDay = start.getDayOfWeek().getValue();

int endDay = end.getDayOfWeek().getValue();

int diff = weeks * ignore.size();

for (DayOfWeek day : ignore) {

int currDay = day.getValue();

if (startDay <= currDay) {

diff++;

}

if (endDay > currDay) {

diff++;

}

}

if (endDay > startDay) {

diff -= endDay - startDay;

}

return days - diff;

}

return days;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值