首页 > 试题广场 >

计算日期到天数转换

[编程题]计算日期到天数转换
  • 热度指数:161846 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
根据输入的日期,计算是这一年的第几天。
保证年份为4位数且日期合法。
进阶:时间复杂度:,空间复杂度:

输入描述:

输入一行,每行空格分割,分别是年,月,日



输出描述:
输出是这一年的第几天
示例1

输入

2012 12 31

输出

366
示例2

输入

1982 3 4

输出

63
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line = br.readLine();
        String[] split = line.split(" ");
        int year = Integer.parseInt(split[0]);
        int month = Integer.parseInt(split[1]);
        int day = Integer.parseInt(split[2]);

        System.out.println(getDays1(year, month, day));
        /*System.out.println(getDays2(year, month, day));
        System.out.println(getDays3(year, month, day));*/
    }

    /**
     * 通过日历类计算
     *
     * @param year
     * @param month
     * @param day
     * @return
     */
    private static int getDays1(int year, int month, int day) {
        // 创建日历类对象
        Calendar calendar = Calendar.getInstance();
        // 设置日历,month从0开始,所以要-1
        calendar.set(year, month - 1, day);

        // 计算当前日期为该年的多少天
        return calendar.get(Calendar.DAY_OF_YEAR);
    }

    /**
     * 通过日期类计算
     *
     * @param year
     * @param month
     * @param day
     * @return
     */
    private static long getDays2(int year, int month, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day);

        // 转换为日期类
        Date time1 = calendar.getTime();
        calendar.set(year, 0, 1);
        Date time2 = calendar.getTime();
        // 日期对象转换为毫秒进行计算
        long l = time1.getTime() - time2.getTime();
        // 换算成天数
        return (1 + l / (1000 * 3600 * 24));
    }

    /**
     * 通过LocalDate计算
     *
     * @param year
     * @param month
     * @param day
     * @return
     */
    private static int getDays3(int year, int month, int day) {
        LocalDate localDate = LocalDate.of(year, month, day);
        int dayOfYear = localDate.getDayOfYear();
        return dayOfYear;
    }
}

发表于 2023-08-12 10:38:10 回复(0)
import java.util.Scanner;
import java.time.LocalDate;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int year = in.nextInt();
        int month = in.nextInt();
        int days = in.nextInt();
        LocalDate localDate = LocalDate.of(year,month,days);
        int dayOfYear = localDate.getDayOfYear();
        System.out.println(dayOfYear);
    }
}

发表于 2023-06-06 10:11:40 回复(2)
import java.util.Scanner;

public class Main {
    //计算日期到天数转换
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int year = sc.nextInt();
        int month = sc.nextInt();
        int date = sc.nextInt();
        int sum = 0;
        int[] arr1 = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30};
        int[] arr2 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};
        if (leap_year(year)) {
            for (int i = 0; i < month - 1; i++) {
                sum += arr1[i];
            }
        } else {
            for (int i = 0; i < month - 1; i++) {
                sum += arr2[i];
            }
        }
        System.out.println(sum + date);
    }
    public static boolean leap_year(int year) {
        if ((year % 100 != 0 && year % 4 == 0) || (year % 400 == 0)) {
            return true;
        }
        return false;
    }
}
发表于 2023-04-25 13:57:21 回复(0)
Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            String[] strs = str.split(" ");
            int year = Integer.parseInt(strs[0]);
            int month = Integer.parseInt(strs[1]);
            int day = Integer.parseInt(strs[2]);
            int countDay = 0;
            if(month==1){
                countDay = day;
            }else if(month==2){
                countDay = 31+day;
            }else if(month==3){
                countDay = 31+28+day;
            }else if(month==4){
                countDay = 31+28+31+day;
            }else if(month==5){
                countDay = 31+28+31+30+day;
            }else if(month==6){
                countDay = 31+28+31+30+31+day;
            }else if(month==7){
                countDay = 31+28+31+30+31+30+day;
            }else if(month==8){
                countDay = 31+28+31+30+31+30+31+day;
            }else if(month==9){
                countDay = 31+28+31+30+31+30+31+31+day;
            }else if(month==10){
                countDay = 31+28+31+30+31+30+31+31+30+day;
            }else if(month==11){
                countDay = 31+28+31+30+31+30+31+31+30+31+day;
            }else if(month==12){
                countDay = 31+28+31+30+31+30+31+31+30+31+30+day;
            }
            if(year%4==0){
                if((year%100==0) &&(year%400!=0)){                  
                }else{
                    //2月后加一天
                    if(month>2){
                        countDay++;
                    }
                }
            }
            System.out.println(countDay);
        }
发表于 2023-04-16 20:47:41 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int year = in.nextInt(), month = in.nextInt(), day = in.nextInt(), sum = 0;
        int[] months = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 11};
        if(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
            months[1] = 29;
        for(int i = 1; i < month; i++){
            sum += months[i-1];
        }
        sum += day;
        System.out.println(sum);
    }
}

发表于 2023-04-09 20:04:24 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    static int days[] = new int[] {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别

        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int year = in.nextInt();
            int month = in.nextInt();
            int date = in.nextInt();
            int sum = 0;
            for (int i = 0; i < month; i++) {
                sum += days[i];
            }
            sum += date;
            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
                if (month > 3) {
                    sum++;
                }
            }
            System.out.println(sum);
        }
    }
}

发表于 2023-03-12 18:55:42 回复(1)
主要是判断闰年
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while(in.hasNext()){
            int nian = in.nextInt();
            int yue = in.nextInt();
            int ri = in.nextInt();
            HashMap<Integer,Integer> map = new HashMap<>();
            map.put(1,31);
            // map.put(2,31);
            map.put(3,31);
            map.put(4,30);
            map.put(5,31);
            map.put(6,30);
            map.put(7,31);
            map.put(8,31);
            map.put(9,30);
            map.put(10,31);
            map.put(11,30);
            map.put(12,31);
            //1、能被4整除并且不能被100整除
            //2、能被400整除
            if(((nian%4)==0&&(nian%100)!=0)||(nian%400==0)){
                map.put(2,29);
            }else{
                map.put(2,28);
            }
            int count = 0;
            for(int i=1;i<=yue;i++){
            
                if(i==yue){
                    count+=ri;
                }else{
                    count+=map.get(i);
                }
            }

            System.out.println(count);

        }

    }
}

发表于 2023-01-08 16:10:29 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int year = sc.nextInt();
        int month = sc.nextInt();
        int day = sc.nextInt();
        if(month<=2){
            System.out.println((month-1)*31+day);
        }
        if(month>2){
            
            if(isLeapYear(year)){
                System.out.println(countDays(month,day)+1);
            }
            else{System.out.println(countDays(month,day));}
        }

        
    }

    public static boolean isLeapYear(int y){
        if(y==1900){
            return false;
        }
        if((y-1952)%4==0){
            return true;
        }
        return false;
    }
    public static int countDays(int month,int day){
        int[] months = {31,28,31,30,31,30,31,31,30,31,30,31};
        int count = 0;
        for(int i = 0;i<month-1;i++){
            count += months[i];
        }
        return count+day;
    }
}
重修小学日历部分去了,1900这个年份真的崽种,好在只有一个整数年不是闰年的
发表于 2022-10-05 20:27:40 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import java.text.ParseException;

public class Main {
    public static void main(String[] args) throws IOException,ParseException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy MM dd");
        Date date = dateFormat.parse(str);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        System.out.println(cal.get(Calendar.DAY_OF_YEAR));
    }
}
发表于 2022-09-12 19:09:53 回复(0)

前缀和数组

import java.io.*;
import java.io.IOException;
import java.util.*;

public class Main {
    private final static int[] dayOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    private final static int[] preSum = new int[dayOfMonth.length + 1];


    public static void main(String[] args) throws IOException {
        for (int i = 1; i < preSum.length; ++i) {
            preSum[i] = preSum[i - 1] + dayOfMonth[i - 1];
        }
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while(Objects.nonNull(str = br.readLine())) {
            String[] array = str.split(" ");
            int year = Integer.parseInt(array[0]),
                month = Integer.parseInt(array[1]),
                day = Integer.parseInt(array[2]);
            boolean isRun = ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
            if (isRun && month > 2) day += preSum[month - 1] + 1;
            else day += preSum[month - 1];
            System.out.println(day);
        }
    }
}
发表于 2022-09-07 21:37:58 回复(0)
第一次100%,记录一下
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] params = br.readLine().split(" ");
        int year = Integer.parseInt(params[0]);
        int month = Integer.parseInt(params[1]);
        int day = Integer.parseInt(params[2]);
        int sum = 0;
        boolean flag = isRunNian(year);
        for(int i = 1; i <= month; i++){
            sum += monthToDay(i - 1 , flag);
        }
        sum += day;
        System.out.println(sum);
    }
    
    public static boolean isRunNian(int year){
        if((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0)){
            return true;
        }
        return false;
    }
    public static int monthToDay(int month, boolean flag){
        switch(month){
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if(flag){
                    return 29;
                }else{
                    return 28;
                }        
        }
        return 0;
    }
}


发表于 2022-08-16 21:51:49 回复(0)
1945 11 2 不应该是305天吗?我手动计算也是305天,为啥预期306啊
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input = br.readLine().split(" ");
        String year = input[0];
        String mon = input[1];
        String day = input[2];
        int days = 0;
        for (int i = Integer.parseInt(mon) - 1 ; i >= 1; i--) {
            if (i % 2 == 0 && i != 2) {
                days += 30;
            } else if (i == 2) {
                if (Integer.parseInt(year) % 4 == 0 && Integer.parseInt(year) % 100 != 0 || Integer.parseInt(year) % 400 == 0) {
                    days += 29;
                } else {
                    days += 28;
                }
            } else {
                days += 31;    
            }
        }
        
        days += Integer.parseInt(day);
        System.out.println(days+"");
    }
}



发表于 2022-07-13 01:12:09 回复(3)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int year = sc.nextInt();
        int month = sc.nextInt();
        int day = sc.nextInt();
        int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int[] months = new int[days.length];
        int sum = 0;
        for (int i = 0; i < days.length; i++) {
            months[i] = sum;
            sum += days[i];
        }
        int flag = 0;
        if ((year % 100 != 0 && year % 4 == 0 || year % 400 == 0) && month > 2) flag++;
        System.out.println(flag + months[month - 1] + day);
    }
}

发表于 2022-07-02 13:28:06 回复(0)
import java.util.*;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int year = sc.nextInt();
            int month = sc.nextInt();
            int day = sc.nextInt();
            Integer[] month_day = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            int sum = 0;

            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                month_day[1] = 29;
            }

            if (month > 0 && month < 13) {
                if (day > 0 && day <= month_day[month - 1]) {
                    if (month > 1) {
                        for (int t = 0; t < month - 1; t++) {
                            sum += month_day[t];
                        }
                    }
                    sum += day;
                }
            }
            System.out.println(sum);
        }
    }
}
发表于 2022-06-26 16:33:34 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[]args){
        Scanner scan = new Scanner(System.in);
        int year = scan.nextInt();
        int month = scan.nextInt();
        int day = scan.nextInt();
        int sum =0;
        int[]array={31,28,31,30,31,30,31,31,30,31,30,31};
        for(int i=0;i<month-1;i++){
            sum+=array[i];
        }
        sum+=day;
        if(isRunYear(year)&&month>2){
            sum =sum+1;
        }
        System.out.println(sum);
    }
    
    public static boolean isRunYear(int year){
        if(year%4==0&&year%100!=0){
            return true;
        }else if(year%100==0&&year%400==0){
            return true;
        }
        return false;
    }
}

发表于 2022-06-21 23:33:18 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
// 没看出哪里有问题,有一个用例没过???
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String nextLine = sc.nextLine();
        String[] split = nextLine.split(" ");
        HashMap<Integer, Integer> hashMap = new HashMap();
        hashMap.put(1, 0);
        hashMap.put(2, 31);
        hashMap.put(3, 31 + 28);
        hashMap.put(4, 31 + 28 + 31);
        hashMap.put(5, 31 + 28 + 31 + 30);
        hashMap.put(6, 31 + 28 + 31 + 30 + 31);
        hashMap.put(7, 31 + 28 + 31 + 30 + 31 + 30);
        hashMap.put(8, 31 + 28 + 31 + 30 + 31 + 30 + 31);
        hashMap.put(9, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31);
        hashMap.put(10, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30);
        hashMap.put(11, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31);
        hashMap.put(12, 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30);
        Integer days = hashMap.get(Integer.parseInt(split[1]));
        int finalNum = Integer.parseInt(split[2]) + days;
        int yeas = Integer.parseInt(split[0]);
        int yue = Integer.parseInt(split[1]);
        if (yeas % 100 == 0) {
            if (yeas % 400 == 0 && yue >= 3){
                System.out.println(finalNum + 1);
            }else {
                System.out.println(finalNum);
            }
        }
        if (yeas % 4 == 0 && yue >= 3) {
            System.out.println(finalNum + 1);
        } else {
            System.out.println(finalNum);
        }
    }
}
发表于 2022-06-15 10:02:29 回复(0)
import java.util.Scanner;

/**
题目:输入日期,计算是这一年的第几天
分析:
 1.按空格,解析出年月日
 2.判断今年是不是润年的方法:不能被100整除但是必须被4整除  或  能被400整除
 3.写出闰年和非闰年的两个int数组,每一个元素代表每一个月的天数
 4.遍历月数,每月对应的最大天数为:int[0到month-1-1] 累加起来
 比如:12月,那么遍历数组中,1-11月,进行累加,对应数组索引为0-10
 最后加上日就是答案了
 **/
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();

        int[] runArr = {31,29,31,30,31,30,31,31,30,31,30,31};
        int[] notRunArr = {31,28,31,30,31,30,31,31,30,31,30,31};

        String[] date = input.split(" ");
        int year = Integer.parseInt(date[0]);
        int month = Integer.parseInt(date[1]);
        int day = Integer.parseInt(date[2]);


        boolean flag = isRun(year);

        int result = 0;
        //计算前n个月的天数
        //12月,那么就要计算 1-11月的,那么对应的索引就是 0-10
        if(flag){
            for (int i = 0; i < month-1; i++) {
                result += runArr[i];
            }
        }else{
            for (int i = 0; i < month-1; i++) {
                result += notRunArr[i];
            }
        }
        //加上日
        result += day;

        System.out.println(result);

    }


    public static boolean isRun(int year){
        if(year%400 == 0){
            return true;
        }else if(year%100 !=0 && year%4==0){
            return true;
        }
        return false;
    }
}

发表于 2022-04-23 15:45:29 回复(0)
可以说是没有任何技巧了
import java.util.Scanner;

public class Main{
    public static int yearTransform(int year, int month){
        switch(month){
            case 1: return 31;
            case 2: {
                if(year%4 == 0 && year%100 != 0)return 29;
                else return 28;
            }
            case 3: return 31;
            case 4: return 30;
            case 5: return 31;
            case 6: return 30;
            case 7: return 31;
            case 8: return 31;
            case 9: return 30;
            case 10: return 31;
            case 11: return 30;
            case 12: return 31;
            default: return 0;
        }
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int year = sc.nextInt();
        int month = sc.nextInt();
        int day = sc.nextInt();
        
        for(int i = 1; i<month; i++){
            day += yearTransform(year, i);
        }
        System.out.println(day);
    }
}


发表于 2022-04-21 21:08:56 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int y = sc.nextInt();
        int m = sc.nextInt();
        int d = sc.nextInt();
        int[] month = new int[]{31,28,31,30,31,30,31,31,30,31,30,31};
        if((y % 4 == 0 && y % 100 != 0) ||(y % 400 ==0)){
            month[1] = 29;
        }
        int day = 0;
        for(int i = 0;i < m - 1;i++){
            day += month[i];
        }
        day +=d;
        System.out.println(day);
    }
}

发表于 2022-04-05 14:45:57 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            String s = sc.nextLine();
            String[] s1 = s.split(" ");
            solution(Integer.parseInt(s1[0]), Integer.parseInt(s1[1]), Integer.parseInt(s1[2]));
        }
    }

    private static void solution(int year, int month, int day) {
        int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if (isLeapYear(year)) {
            days[1] = 29;
        }
        int daysToNow = 0;
        for (int i = 0; i < month - 1; i++) {
            daysToNow += days[i];
        }
        daysToNow += day;
        System.out.println(daysToNow);
    }

    private static boolean isLeapYear(int year) {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }
}

发表于 2022-03-27 14:38:23 回复(0)

问题信息

难度:
64条回答 31887浏览

热门推荐

通过挑战的用户

查看代码