Java日期查找日

时间:2017-10-26 08:23:40

标签: java weekday

我需要知道特定日期的日期和日期名称。不知何故,我犯了一个错误,因为多年来19岁......它的确有效,但在2000年,我再也无法得到正确的一天了。例如,如果我使用日期9.1.2001它当天没有说我,而是发生了我所做的开关错误。

我不应该使用日历方法

这是我的代码:

public class FindDay {

    public static void main(String[] args) {
        System.out.println("Type a day: ");
        int day = In.readInt();
        System.out.println("Type a month: ");
        int month = In.readInt();
        System.out.println("Type a year: ");
        int year = In.readInt();

        if(day <  1 || day > 32) {
            System.out.println("Type valid day");
        }else if (month < 1 || month >12) {
            System.out.println("Type valid month");
        }else if (year < 1900) {
            System.out.println("Type valid year");
        }

        int wholeDaysYear; //to calculate how much days a year has

        if (year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0) ) {
            wholeDaysYear = 366;
        }else {
            wholeDaysYear = 365;
        }
        int monthDays = 0;  //calculates days to this month
        int february;       //if February has 28 or 2 days
        if(wholeDaysYear ==366) {
            february = 29;
        }else {
            february = 28;
        }
        switch(month) { 
        case 1: monthDays= 0; break;
        case 2: monthDays =31; break;
        case 3: monthDays= 31+february; break;
        case 4: monthDays= 31+february+31; break;
        case 5: monthDays= 31+february+31+30; break;
        case 6: monthDays= 31+february+31+30+31; break;
        case 7: monthDays= 31+february+31+30+31+30; break;
        case 8: monthDays= 31+february+31+30+31+30+31; break;
        case 9: monthDays= 31+february+31+30+31+30+31+31; break;
        case 10: monthDays= 31+february+31+30+31+30+31+31+30; break;
        case 11: monthDays= 31+february+31+30+31+30+31+31+30+31; break;
        case 12: monthDays= 31+february+31+30+31+30+31+31+30+31+30; break;
        default: System.out.println("Enter valid month!");break;
        }

        int leapYear = ((year-1) / 4 - 474) - ((year-1) / 100 - 18) + ((year-1) / 400 - 4); //calculates the leap years
        int allDays =(((year - leapYear)-1900)*wholeDaysYear)+monthDays+day-1; //Calculates all days

        System.out.println(allDays);

        int dayName = allDays % 7;
        String dayNames = null; //gives the days their name
        switch (dayName) {
        case 1: dayNames = "Monday";break;
        case 2: dayNames = "Tuesday";break;
        case 3: dayNames = "Wendesday";break;
        case 4: dayNames = "Thursday";break;
        case 5: dayNames = "Friday";break;
        case 6: dayNames = "Saturday";break;
        case 7: dayNames = "Sunday";break;
        default: System.out.println("Type valid Input");break;
        }

        System.out.println(dayNames);
    }
}

2 个答案:

答案 0 :(得分:2)

你不需要这么努力工作......假设你有一个月中的某一天,一年中的一个月和全年,你可以简单地做(例子):

    LocalDate ld = LocalDate.of(1999, 12, 12);
    System.out.println(ld.getDayOfWeek()); // prints SUNDAY
自Java 8以来,

LocalDate可用。

至于你的代码,很容易发现的两个错误是:

  1. 交换机中的情况从1到7,而它们应该从0到6,因为你正在计算7中的提醒。
  2. int dayName = allDays % 7;在这里你假设一年的第一天是星期一(根据你的案例转换),这不一定是真的。

答案 1 :(得分:0)

您可以尝试这样的事情:

    Date yourDate; 
    Calendar cal = Calendar.getInstance();
    cal.setTime(yourDate);
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH); //or Calendar.DAY_OF_WEEK
    // etc.