计算任意两个日期之间的天数问题

 程序代码:

#include  " stdio.h "

int  days[ 2 ][ 13 ] = {365,31,28,31,30,31,30,31,31,30,31,30,31,
                
366,31,29,31,30,31,30,31,31,30,31,30,31}
;

int  Leap( int  year)         // 判断是否为闰年
{
    
return ( (year%400==0|| (year%4==0 && year%100!=0));
}


int  Deal( int  dat0, int  dat1)
{
    
int i,temp,sum0,sum;
    
int year0,month0,day0,year1,month1,day1;
    sum0 
= sum = 0;
    year0 
= dat0/10000;    month0 = dat0/100%100;    day0 = dat0%100;
    year1 
= dat1/10000;    month1 = dat1/100%100;    day1 = dat1%100;

    
/*************************************************************/
    
/* 在起始日期和终止日期在同一年                                         */
    
/*************************************************************/
    
if((year1-year0)==0)
    
{
        
if((month1-month0)==0)
            
return (day1-day0);
        
else
        
{
            temp 
= Leap(year0);
            sum 
= days[temp][month0] - day0 +1;
            
for(i=month0+1;i<=month1-1;i++)
                sum 
= sum + days[temp][i];
            sum 
=sum + day1;
            
return sum;
        }
        
    }


    
/************************************************************************/
    
/* 在起始日期和终止日期不在同一年                                       */
    
/************************************************************************/
    
//计算开始年剩余的天数
    temp = Leap(year0);
    sum0 
= sum0 + (days[temp][month0] - day0) + 1;
    
for (i=month0+1; i<=12; i++)
        sum0 
= sum0 + days[temp][i];
    
    sum 
= sum +sum0;
    sum0 
= 0;

    
//计算中间年份的总天数
    for (i=year0+1;i<=year1-1;i++)
    
{
        temp 
= Leap(i);
        sum 
= sum + days[temp][0];
    }


    
//计算结束年过去的天数
    temp = Leap(year1);
    
for(i=1;i<=month1-1;i++)
        sum0 
= sum0 + days[temp][i];
    sum0 
= sum0 + day1;

    sum 
= sum + sum0;

    
return sum;
}


void  main()
{
    
int dat0,dat1,sum;
    FILE 
*fp = NULL;
    
if(fp = fopen("riqi.dat","r"))
    
{
        
while (fscanf(fp,"%d%d",&dat0,&dat1)==2)
        
{
            
if(dat0==0 && dat1==0)    break;
            sum 
= Deal(dat0,dat1);
            printf(
"天数为:%d ",sum);
        }

    }

    fclose(fp);
}

程序数据文件格式:

(日期格式为:年月日;年为任意位数,月、日的位数为两位,不足补零;两个日期中间以空格隔开;以下数据复制到记事本,重命名为“riqi.dat”,与代码文件放在同一文件夹)

  例:

19861231 19870101
19860101 19861231
19850101 19861231
19840101 19861231
19830101 19861231
19820101 19861231
19810101 19861231
2020811 19860606
0 0

 

运算结果:

天数为:2
天数为:365
天数为:730
天数为:1096
天数为:1461
天数为:1826
天数为:2191
天数为:651528
请按任意键继续. . .

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中计算任意两个日期之间的工作天数可以使用以下步骤: 1. 首先需要计算两个日期之间天数差。 2. 接下来需要计算出这个时间段内有多少个周末。一般情况下周六和周日是周末,但是不同国家可能定义的周末不同,需要根据具体情况进行修改。 3. 最后用时间段的天数减去周末的天数,就是工作天数。 下面是一个示例代码实现: ```java import java.time.DayOfWeek; import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class WorkingDaysCalculator { public static void main(String[] args) { LocalDate startDate = LocalDate.of(2021, 1, 1); LocalDate endDate = LocalDate.of(2021, 1, 31); int workingDays = calculateWorkingDays(startDate, endDate); System.out.println("Working days between " + startDate + " and " + endDate + ": " + workingDays); } public static int calculateWorkingDays(LocalDate startDate, LocalDate endDate) { int daysBetween = (int) ChronoUnit.DAYS.between(startDate, endDate); int weekends = 0; for (int i = 0; i <= daysBetween; i++) { DayOfWeek dayOfWeek = startDate.plusDays(i).getDayOfWeek(); if (dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY) { weekends++; } } return daysBetween - weekends + 1; // Add 1 to include the start date } } ``` 在这个示例中,我们计算了2021年1月1日到2021年1月31日之间的工作日数,输出结果为21。注意,这个示例中假设周六和周日是周末。如果需要适应不同的周末定义,需要修改代码中的相应部分。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值