我正在做一个项目,包括:'编写一个提示日期(月,日,年)的程序,并报告该日期的星期几。有可能 有助于知道1601年1月1日是星期一。'。这是一本“构建Java程序 - 回归基础知识方法,第2版”的练习,这本书是我自学Java自学的书。任何反馈都受到高度赞赏,但我确实要求你解释为什么你会以某种方式做某事。谢谢!
所以,我的问题是,对于接近1600年的日期,它给出了正确的一天(我相信),最近几天也是如此,他们有三天的偏移(至少我是选中)。为什么会发生这种情况,我该如何解决?谢谢!
我的代码:
// finds the day of the week of the given date
public static String dayFinder(int month, int day, int year) {
// handle invalid input
if (month > 12 || month < 1 || day > 31 || day < 1) {
throw new IllegalArgumentException("Month must be between "
+ "1 and 12 and Day must be between 1 and 31.");
}
// convert to "absolute" day, covering day and month
int absoluteDay = monthToDay(month, day, year);
// convert year to days and add to "absolute" day
absoluteDay += yearToDay(year);
if (absoluteDay % 7 == 1) {
return "Monday";
} else if (absoluteDay % 7 == 2) {
return "Tuesday";
} else if (absoluteDay % 7 == 3) {
return "Wednesday";
} else if (absoluteDay % 7 == 4) {
return "Thursday";
} else if (absoluteDay % 7 == 5) {
return "Friday";
} else if (absoluteDay % 7 == 6) {
return "Saturday";
} else { // absoluteDay % 7 == 0
return "Sunday";
}
}
// calculates the number of days present in a given
// date since the beginning of the year
public static int monthToDay(int month, int day, int year) {
// convert to "absolute" day
int absoluteDay = 0, daysTo31 = 0;
// iterate through months
for (int i = 0, loopMonth = month; i < month; i++) {
if (loopMonth == 1 || loopMonth == 3 || loopMonth == 5
|| loopMonth == 7 || loopMonth == 8 || loopMonth == 10
|| loopMonth == 12) {
absoluteDay += 31;
daysTo31 = 0;
} else if (loopMonth == 2) {
if (year % 4 != 0) {
absoluteDay += 28;
daysTo31 = 3;
} else { // leap year
absoluteDay += 29;
daysTo31 = 2;
}
} else { // month = 4, 6, 9 or 10
absoluteDay += 30;
daysTo31 = 1;
}
loopMonth--;
}
// adjust to specific day
absoluteDay -= (31 - day - daysTo31);
return absoluteDay;
}
// calculates the number of days between
// (the beginning of) the given year and
// (the beginning of) the reference year 1601
public static int yearToDay(int year) {
// convert to "absolute" day
int absoluteDay = 0;
year -= 1601;
// iterate through years
for (int i = 0, loopYear = year; i < year; i++) {
if (loopYear % 4 != 0) {
absoluteDay += 365;
} else { // leap year
absoluteDay += 366;
}
loopYear--;
}
return absoluteDay;
}
// 1604年(MDCIV)是从周四开始的闰年
答案 0 :(得分:5)
你的问题可能与闰年有关。
由于维基百科:
Every year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100; the centurial years that are exactly divisible by 400 are still leap years. For example, the year 1900 is not a leap year; the year 2000 is a leap year.
[link]
这就是为什么你有三天太多(1700,1800,1900)。
答案 1 :(得分:1)
Java.util日期/日历功能充满了疯狂。我认为Java7已经改进了一点(我听说过,但没有调查过),但我推荐第三方库Joda-Time。做你想和Joda做的事情是:
DateTime anyDateTime = new DateTime(
1800, //year
4, //month
19, //day
0, //hour
0); // minutes
System.out.println("DOW = " + anyDateTime.dayOfWeek().getAsText());
打印“DOW = Saturday” 我意识到你的目标是学习Java,但并不总是需要重新发明轮子。
答案 2 :(得分:0)
int dayOfWeekNumber =
LocalDate.of( 2016 , 12 , 22 ) // A date-only object, no time-of-day nor time zone.
.getDayOfWeek() // `DayOfWeek` enum object.
.getValue() ; // 1-7 for Monday-Sunday.
问题实际上是关于算法的。但是,仅供参考,这个功能是使用java.time类构建到Java中的。
LocalDate
LocalDate
类表示没有时间且没有时区的仅限日期的值。
LocalDate ld = LocalDate.of( 2016 , 12 , 22 );
DayOfWeek
DayOfWeek
枚举为一周中的每一天定义七个对象。
DayOfWeek dow = ld.getDayOfWeek() ;
通常最好使用DayOfWeek
对象。但如果你绝对需要一个整数,你可以要求一个。星期一至星期日每ISO 8601编号为1-7。
int dowNumber = dow.getValue();
java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧日期时间类,例如java.util.Date
,.Calendar
和&amp; java.text.SimpleDateFormat
现在位于Joda-Time的maintenance mode项目建议迁移到java.time。
要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。
大部分java.time功能都被反向移植到Java 6&amp; ThreeTen-Backport中的7,并进一步适应Android中的ThreeTenABP(见How to use…)。
ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如Interval
,YearWeek
,YearQuarter
和more。
答案 3 :(得分:0)
import java.util.*;
此代码适用于任何日期。
也许会帮助某人。
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner console = new Scanner(System.in);
System.out.println("Give me a special date !!!");
System.out.print("Month: ");
int month = console.nextInt();
System.out.print("Day: ");
int day = console.nextInt();
System.out.print("Year:");
int year = console.nextInt();
String date = dayFinder(month, day, year);
System.out.println("It is the date " + month + "/" + date + "/" + year);
console.close();
}
public static String dayFinder(int month, int day, int year) {
// handle invalid input
if (month > 12 || month < 1 || day > 31 || day < 1) {
throw new IllegalArgumentException("Month must be between "
+ "1 and 12 and day must be between 1 and 31");
} else {
// convert to "absolute" day, covering day and month
int absoluteDay = monthToDay(month, day, year);
// convert year to days and add to "absolute" day
absoluteDay += yearToDay(year);
if (absoluteDay % 7 == 2) {
return "Monday";
} else if (absoluteDay % 7 == 3) {
return "Tuesday";
} else if (absoluteDay % 7 == 4) {
return "Wednesday";
} else if (absoluteDay % 7 == 5) {
return "Thursday";
} else if (absoluteDay % 7 == 6) {
return "Friday";
} else if (absoluteDay % 7 == 0) {
return "Saturday";
} else { // absoluteDay % 7 == 1
return "Sunday";
}
}
}
// calculates the number of days between
// (the beginning of) the given year
// (the beginning of) the reference year 1601;
public static int yearToDay(int years) {
// covert to "absolute" day;
int absoluteDay = 0;
int leapYears = 0;
// iterate through years;
for (int i = 0; i < years; i++) {
if (((i % 4) == 0) && ((i % 100) != 0)) {
leapYears +=1;
} else if (i % 400 == 0) {
leapYears ++;
}
}
absoluteDay = (leapYears * 366) + (((years - 1) - leapYears) * 365);
return absoluteDay;
}
// Calculates the numbers of days present in a given
// date since the beginning of the year;
public static int monthToDay (int month, int day, int year) {
// convert to absolute day;
int absoluteDay = 0;
// iterate through months
for (int i = 1; i < month; i++) {
if ((i == 4) || (i == 6) || (i == 9) || (i == 11)) { // 30 day
absoluteDay += 30;
} else if (i == 2) {
if ((year % 4 == 0) && (year % 100 != 0)) {
absoluteDay += 29;
} else if (year % 400 == 0) {
absoluteDay += 29;
} else {
absoluteDay += 28;
}
} else {
absoluteDay += 31; // 1,3,5,7,8,10,12 months
}
}
return absoluteDay + day;
}
}