需要使用java.util.calendar创建没有日历的日历事件

需要使用java.util.calendar创建没有日历的日历事件,java,events,calendar,Java,Events,Calendar,我的任务是制作一个代码来打印日历,如下图所示: 用户必须输入月份和年份。下面的代码显示了我需要的内容,但只添加了添加事件和显示事件的方法。我不知道从哪里开始,怎么开始?我认为最好的办法是将数据保存到某个文件或其他地方 public static int day(int M, int D, int Y) { int y = Y - (14 - M) / 12; int x = y + y/4 - y/100 + y/400; int m = M + 12 * ((14 -

我的任务是制作一个代码来打印日历,如下图所示:

用户必须输入月份和年份。下面的代码显示了我需要的内容,但只添加了添加事件和显示事件的方法。我不知道从哪里开始,怎么开始?我认为最好的办法是将数据保存到某个文件或其他地方

public static int day(int M, int D, int Y) {
    int y = Y - (14 - M) / 12;
    int x = y + y/4 - y/100 + y/400;
    int m = M + 12 * ((14 - M) / 12) - 2;
    int d = (D + x + (31*m)/12) % 7;
    return d;
}

// return true if the given year is a leap year
public static boolean isLeapYear(int year) {
    if  ((year % 4 == 0) && (year % 100 != 0)) return true;
    if  (year % 400 == 0) return true;
    return false;
}

public static void main(String[] args) {
    Scanner ulaz = new Scanner(System.in);
    int M = ulaz.nextInt();    // month (Jan = 1, Dec = 12)
    int Y = ulaz.nextInt();    // year

    // months[i] = name of month i
    String[] months = {
        "",                               // leave empty so that months[1] = "January"
        "January", "February", "March",
        "April", "May", "June",
        "July", "August", "September",
        "October", "November", "December"
    };

    // days[i] = number of days in month i
    int[] days = {
        0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
    };

    // check for leap year
    if (M == 2 && isLeapYear(Y)) days[M] = 29;


    // print calendar header
    System.out.println("   " + months[M] + " " + Y);
    System.out.println(" S  M Tu  W Th  F  S");

    // starting day
    int d = day(M, 1, Y);

    // print the calendar
    for (int i = 0; i < d; i++)
        System.out.print("   ");
    for (int i = 1; i <= days[M]; i++) {
        System.out.printf("%2d ", i);
        if (((i + d) % 7 == 0) || (i == days[M])) System.out.println();
    }

}
公共静态整数日(整数M、整数D、整数Y){
int y=y-(14米)/12;
INTX=y+y/4-y/100+y/400;
int m=m+12*((14-m)/12)-2;
int d=(d+x+(31*m)/12)%7;
返回d;
}
//如果给定年份是闰年,则返回true
公共静态布尔值isLeapYear(整数年){
如果((年份%4==0)和(&(年份%100!=0))返回true;
如果(年份%400==0)返回true;
返回false;
}
公共静态void main(字符串[]args){
扫描仪ulaz=新扫描仪(System.in);
int M=ulaz.nextInt();//月份(一月=1,十二月=12)
int Y=ulaz.nextInt();//年
//月份[i]=月份i的名称
字符串[]个月={
“”,//保留为空,以便月[1]=“一月”
“一月”、“二月”、“三月”,
“四月”、“五月”、“六月”,
“七月”、“八月”、“九月”,
“十月”、“十一月”、“十二月”
};
//天数[i]=第i个月的天数
int[]天={
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
//检查闰年
如果(M==2&&isLeapYear(Y))天[M]=29;
//打印日历标题
System.out.println(“+months[M]+”+Y);
System.out.println(“S M Tu W Th F S”);
//开始日
int d=天(M,1,Y);
//打印日历
对于(int i=0;i对于(int i=1;i,您可以在这里找到一个很好的教程,介绍如何从头开始制作日历,包括事件显示。它是法语的,但完整的代码可以在注释中找到:

关于您的问题,最好是将事件存储在数据库中。 基本上,您应该创建一个具有列的表,如下所示: id\u事件、日期\u事件、时间\u事件、描述\u事件…以及其他需要的详细信息

然后,您可以通过php脚本查询数据库(参见php pdo教程)。最终,您可以通过javascript和ajax调用php脚本来添加、存储和修改事件。我个人通过函数getJSON使用jQuery


祝你好运:)

为什么你不能使用
java.util.Calendar
?你可以使用其他API,如Joda Time或
java.Time
?目前你问的问题很不清楚。@Jon Skeet我的老师希望这样,不要使用java.util.Calendar…但是如果有人可以使用java.util.Calendar,我想看看它是如何工作的。