根据日期判断是第几天

题目描述:输入一个日期,判断该日期是该年的第几天

package com.wyj.interview.realtopic;

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * @description: 输入一个年月日,输出该日期是这一年的第几天
 * @author wangyijun01_sx@qiyi.com
 * @date 2018/12/29 14:19
 */
public class getDaysByDate {

    private static final Integer bigMonth = 31;
    private static final Integer smallMonth = 30;
    private static Set<String> BIG_MONTHS = new HashSet<>();
    private static Set<String> SMALL_MONTHS = new HashSet<>();
    static {
        BIG_MONTHS.add("01");
        BIG_MONTHS.add("03");
        BIG_MONTHS.add("05");
        BIG_MONTHS.add("07");
        BIG_MONTHS.add("08");
        BIG_MONTHS.add("10");
        BIG_MONTHS.add("12");

        SMALL_MONTHS.add("04");
        SMALL_MONTHS.add("06");
        SMALL_MONTHS.add("09");
        SMALL_MONTHS.add("11");
    }

    public static void main(String[] args) {
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入日期(格式:yyyy-MM-dd)");
            String dateStr = sc.next();
            System.out.println(getTotalDays(dateStr));
        } catch (Exception e) {
            System.out.println("格式输入错误,请重新输入");
            e.printStackTrace();
        }

    }

    private static Integer getTotalDays(String date) {


        String[] strs = date.split("-");
        String yearStr = strs[0];
        Integer year = Integer.valueOf(yearStr);
        String month = strs[1];
        String day = strs[2];
        Integer days = Integer.valueOf(day);
        Integer totalDays = 0;
        switch (month) {
            case "12":
                totalDays += bigMonth;
            case "11":
                totalDays += smallMonth;
            case "10":
                totalDays += bigMonth;
            case "09":
                totalDays += smallMonth;
            case "08":
                totalDays += bigMonth;
            case "07":
                totalDays += bigMonth;
            case "06":
                totalDays += smallMonth;
            case "05":
                totalDays += bigMonth;
            case "04":
                totalDays += smallMonth;
            case "03":
                totalDays += bigMonth;
            case "02":
                if ((year % 100 != 0 && year % 4 == 0) || (year % 100 == 0 && year % 400 == 0)) {
                    totalDays += 29;
                }
                else {
                    totalDays += 28;
                }
            case "01":
                totalDays += bigMonth;
                break;
        }

        Integer temp = 0;
        if (BIG_MONTHS.contains(month)) {
            temp = bigMonth;
        } else if (SMALL_MONTHS.contains(month)) {
            temp = smallMonth;
        }else {
            return days + bigMonth;
        }

        return totalDays + days - temp;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值