Home > Java > JavaBase > How to calculate the day of the year for a specified date in java

How to calculate the day of the year for a specified date in java

Release: 2019-12-05 11:57:06
Original
4046 people have browsed it

How to calculate the day of the year for a specified date in java

Java input date calculation is the day of the year:

Idea

Use the year to distinguish whether it is a leap year or an ordinary year, February 28 in an ordinary year days, February has 29 days in leap years;

1, 3, 5, 7, 8, 10, and December have 31 days, and the other months have 30 days;

Then add the number of days in each month Just add them together. Note that if you enter December, it will be accumulated from November to January, and the number of days entered in January will be added;

Implementation code:

import java.util.Scanner;

/**
 * Created by xpf on 2018/6/22 :)
 * GitHub:xinpengfei520
 * Function:
 */
public class CalculateUtils {

    /*平年二月28天*/
    private static final int DAYS_28 = 28;
    /*闰年二月29天*/
    private static final int DAYS_29 = 29;
    /*除了31天的月份其他均为30天*/
    private static final int DAYS_30 = 30;
    /*1、3、5、7、8、10、12月份31天*/
    private static final int DAYS_31 = 31;

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Please input year:");
        int year = input.nextInt();
        System.out.println("Please input month:");
        int month = input.nextInt();
        System.out.println("Please input day:");
        int day = input.nextInt();

        int daysInYear = getDaysInYear(year, month, day);
        System.out.println("daysInYear:" + daysInYear);
    }

    /**
     * get days in this year
     *
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static int getDaysInYear(int year, int month, int day) {
        int totalDays = 0;

        switch (month) {
            // 12 月份加的是11月份的天数,依次类推
            case 12:
                totalDays += DAYS_30;
            case 11:
                totalDays += DAYS_31;
            case 10:
                totalDays += DAYS_30;
            case 9:
                totalDays += DAYS_31;
            case 8:
                totalDays += DAYS_31;
            case 7:
                totalDays += DAYS_30;
            case 6:
                totalDays += DAYS_31;
            case 5:
                totalDays += DAYS_30;
            case 4:
                totalDays += DAYS_31;
            case 3:
                // 判断是否是闰年
                if (((year / 4 == 0) && (year / 100 != 0)) || (year / 400 == 0)) {
                    totalDays += DAYS_29;
                } else {
                    totalDays += DAYS_28;
                }
            case 2:
                totalDays += DAYS_31;
            case 1: // 如果是1月份就加上输入的天数
                totalDays += day;
        }

        return totalDays;
    }
}
Copy after login

Because only the number of days in February and the entered day number are not fixed, the number of days in other months is fixed, and the fixed number of days can be calculated from the entered month, so we can calculate it like this:

The fixed number of days calculated from the number of days entered in February

For more java knowledge, please pay attention tojava basic tutorial.

The above is the detailed content of How to calculate the day of the year for a specified date in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template