首页 > 试题广场 >

日期差值

[编程题]日期差值
  • 热度指数:43248 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天

输入描述:
有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD


输出描述:
每组数据输出一行,即日期差值
示例1

输入

20110412
20110422

输出

11
import java.io.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    private static int[] months = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String date1 = bufferedReader.readLine();
        String date2 = bufferedReader.readLine();
        Integer year1 = Integer.parseInt(date1.substring(0, 4));
        Integer month1 = Integer.parseInt(date1.substring(4, 6));
        Integer day1 = Integer.parseInt(date1.substring(6, 8));
        Integer year2 = Integer.parseInt(date2.substring(0, 4));
        Integer month2 = Integer.parseInt(date2.substring(4, 6));
        Integer day2 = Integer.parseInt(date2.substring(6, 8));
        int daynum1 = daynum(year1, month1, day1);
        int daynum2 = daynum(year2, month2, day2);
        int result = 0;
        if(year1.equals(year2)){
            result = daynum2 - daynum1 + 1;
        }else{
            int daysofyear1 = (year1 % 4 == 0 && year1 % 100 != 0) || (year1 % 400 == 0) ? 366 : 365;
            int restofyear1 = daysofyear1 - daynum1;
            int midyear = 0;
            for(int year = year1+1; year < year2; year++){
                int daysofyear = (year1 % 4 == 0 && year1 % 100 != 0) || (year1 % 400 == 0) ? 366 : 365;
                midyear += daysofyear;
            }
            result = restofyear1 + midyear + daynum2 + 1;
        }
        System.out.println(result);
    }

    public static int daynum(int year, int month, int day) {
        months[2] = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ? 29 : 28;
        int daynum = 0;
        for (int i = 1; i < month; i++) {
            daynum = daynum + months[i];
        }
        return daynum + day;
    }
}

发表于 2021-02-22 21:48:59 回复(0)
Java
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            LocalDate date1 = getDate(scanner.next());
            LocalDate date2 = getDate(scanner.next());
            System.out.println(date1.until(date2, ChronoUnit.DAYS)+1);
        }
    }

    static LocalDate getDate(String s){
        String year = s.substring(0, 4);
        String month = s.substring(4, 6);
        String day = s.substring(6);
        return LocalDate.of(Integer.parseInt(year),Integer.parseInt(month),Integer.parseInt(day));
    }
}


发表于 2020-03-18 17:40:20 回复(0)
import java.util.*;
import java.lang.*;
import java.text.*;

public class Main {
    public static void main(String[] args) throws ParseException {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String sq = sc.nextLine();
            String sh = sc.nextLine();
            int[] dd = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
            int year1 = Integer.valueOf(sq.substring(0, 4));
            int yue1 = Integer.valueOf(sq.substring(4, 6));
            int day1 = Integer.valueOf(sq.substring(6));
            int year2 = Integer.valueOf(sh.substring(0, 4));
            int yue2 = Integer.valueOf(sh.substring(4, 6));
            int day2 = Integer.valueOf(sh.substring(6));
            int sum = 0;
            //不同年份处理
            if (!(year1 == year2)) {
                for (int j = year1; j <= year2; j++) {
                    if (isRun(j)) {
                        dd[2] = 29;

                    }
                    if (j == year2) {//end年份处理
                        for (int i = 1; i <= yue2; i++) {
                            if (i == yue2) {
                                sum += day2;
                            } else {
                                sum += dd[i];
                            }
                        }
                    } else if (j == year1) {//start年份处理
                        for (int i = yue1; i <= 12; i++) {
                            if (i == yue1) {
                                sum += dd[i] - day1 + 1;
                            }else{
                                sum += dd[i];
                            }
                            
                        }
                    } else {
                        if (isRun(j)) {//跨整年的年份处理
                            sum += 366;
                        } else {
                            sum += 365;
                        }
                    }
                    dd[2] = 28;

                }
                // 同一年日期处理
            } else {
                if (isRun(year1)) {
                    dd[2] = 29;
                }
                for (int i = yue1; i <= yue2; i++) {
                    if (i == yue1) {
                        sum += dd[i] - day1 + 1;
                    } else if (i == yue2) {
                        sum += day2;
                    } else {
                        sum += dd[i];
                    }
                }
                dd[2] = 28;
            }
            System.out.println(sum);
        }

    }


    public static boolean isRun(int b) {

        return (b % 4 == 0 && b % 100 != 0 || b % 400 == 0);
    }
}
发表于 2019-02-23 17:56:47 回复(0)