Java 月初一
对于家庭作业,我必须做一个全年日历。我设法得到了正确的格式等,但月总是从星期天开始。我必须添加一些东西,使之在一周中正确的一天开始工作 有人能解释一下,我是如何计算(并实现)一个月从一周中的哪一天开始的吗?它用于函数Java 月初一,java,Java,对于家庭作业,我必须做一个全年日历。我设法得到了正确的格式等,但月总是从星期天开始。我必须添加一些东西,使之在一周中正确的一天开始工作 有人能解释一下,我是如何计算(并实现)一个月从一周中的哪一天开始的吗?它用于函数showYear 编程语言是Java 一些限制:我不允许使用预定义的类、日历或日期等,也不允许使用数组 这是我到目前为止的代码: package..; import java.util.Scanner; public class Easter { public static v
showYear
编程语言是Java
一些限制:我不允许使用预定义的类、日历或日期等,也不允许使用数组
这是我到目前为止的代码:
package..;
import java.util.Scanner;
public class Easter {
public static void main(String[] arguments) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the year:");
int year = scanner.nextInt();
showYear(year);
scanner.close();
}
static boolean isLeapYear(int year) {
if (year % 100 == 0)
year = year / 100;
if (year % 4 == 0)
return true;
return false;
}
static int numberOfDaysInMonth(int year, Month month) //this function uses a Class that is made by my professor. But this is not relevant for my question//
{
switch (month) {
case APRIL:
case JUNE:
case SEPTEMBER:
case NOVEMBER:
return 30;
case FEBRUARY:
if (isLeapYear(year))
return 29;
return 28;
default:
return 31;
}
}
static void showYear(int year) {
System.out.println(year);
for (int i = 1; i <= 12; i++) {
System.out.println();
System.out.println(" " + Month.month(i));
System.out.println(" S M T W T F S");
// String firstWeek="";
// how to calculate what day the month starts?
// System.out.print(firstWeek);
int dayOfMonth = 1;
while (dayOfMonth <= numberOfDaysInMonth(year, Month.month(i))) {
for (int k = 0; k < 7 && (dayOfMonth <= numberOfDaysInMonth(year, Month.month(i))); k++) {
System.out.printf("%2d ", dayOfMonth);
dayOfMonth++;
}
System.out.println();
}
}
}
}
package。。;
导入java.util.Scanner;
公共课复活节{
公共静态void main(字符串[]参数){
扫描仪=新的扫描仪(System.in);
System.out.println(“输入年份:”);
int year=scanner.nextInt();
展览年(年);
scanner.close();
}
静态布尔值isLeapYear(整数年){
如果(年份%100==0)
年份=年份/100;
如果(第%4年==0)
返回true;
返回false;
}
静态int numberOfDaysInMonth(int year,Month Month)//此函数使用我的教授创建的类。但这与我的问题无关//
{
开关(月){
四月个案:
案件6月:
案件9月:
案件11月:
返回30;
案件2月:
如果(年)
返回29;
返回28;
违约:
返回31;
}
}
静态无效显示年份(整数年){
系统输出打印项次(年);
对于公历中的(int i=1;i,如果一年可除以4,而不是100或400,则该年为闰年:
(year%4 == 0) && ((year%100 != 0) || (year%400 == 0)).
这将为您提供一年中的天数(365天,或闰年366天)
您可以计算从(虚拟)第0年开始的天数:
从年初到月初的天数为:
0 if month == 1
0+31 if month == 2
0+31+x if month == 3(where x is either 28 or 29 days depending on the year)
0+31+x+31 if month == 4
etc...
现在,为了避免在计算中使用2月份的天数(这不是常数),可以将年份从3月份开始:
int monthIndex;
if (month < 3) {
--year;
monthIndex = month + 12 - 3;
} else {
monthIndex = month - 3;
}
你只需要把一个月的哪一天减去1
总而言之:
public static int days(int year, int month, int day) {
int monthIndex;
if (month < 3) {
--year;
monthIndex = month + 12 - 3;
} else {
monthIndex = month - 3;
}
int yearStart = 365*year + year/4 - year/100 + year/400;
return yearStart + MONTH_START[monthIndex] + day-1;
}
星期天返回0,星期一返回1,依此类推……我的问题有点清楚吗?你需要一个参考,一年中的第一个星期一、十年、世纪、年龄等等,只需选择一个。这样你就可以计算一年中的第一天/星期一。对于家庭作业,我只需对1月1日的星期一进行编码,然后让随后的每个月从这一天开始在上个月结束后。顺便说一句:我不允许使用预定义的类Calendar或Date。无论如何,你不会希望这样,这些类设计糟糕,而且早已过时。如果允许使用日期的现代类LocalDate
,你会想使用它。
static final int[] MONTH_START = {
0, // March
31, // April
31+30, // May
31+30+31, // June
31+30+31+30, // Jully
31+30+31+30+31, // August
31+30+31+30+31+31, // September
31+30+31+30+31+31+30, // October
31+30+31+30+31+31+30+31, // November
31+30+31+30+31+31+30+31+30, // December
31+30+31+30+31+31+30+31+30+31, // January
31+30+31+30+31+31+30+31+30+31+31, // February
};
public static int days(int year, int month, int day) {
int monthIndex;
if (month < 3) {
--year;
monthIndex = month + 12 - 3;
} else {
monthIndex = month - 3;
}
int yearStart = 365*year + year/4 - year/100 + year/400;
return yearStart + MONTH_START[monthIndex] + day-1;
}
public static int dayOfWeek(int year, int month, int day) {
return (days(year, month, day)-4)%7;
}