简体   繁体   English

需要不使用java.util.Calendar进行日历事件

[英]Need to make calendar events with no using java.util.Calendar

I have a task to make a code to print calendar like at the photo below: 我的任务是编写代码以打印日历,如下图所示:

在此处输入图片说明

User has to enter the month and year.Code bellow shows what I need, but only to add methods to add events and show events. 用户必须输入月份和年份。代码下面显示了我需要的内容,但仅添加了添加事件和显示事件的方法。 I don't know where and how to start with it? 我不知道从哪里开始以及如何开始? I think the best idea is to save the data to some file or something. 我认为最好的主意是将数据保存到某个文件或其他内容中。

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();
    }

}

} }

Here you can find a nice tutorial about how to make a calendar from scratch,including events display. 在这里,您可以找到有关如何从头开始制作日历(包括事件显示)的不错的教程。 It is in french but the full code is available in comments: http://www.grafikart.fr/tutoriels/php/calendrier-php-157 它是法语的,但完整的代码可在注释中找到: http : //www.grafikart.fr/tutoriels/php/calendrier-php-157

Regarding your matter, the best is to store your events in a database. 关于您的问题,最好的方法是将事件存储在数据库中。 Basically you should create a table with colums as follow: id_event, date_event, time_event, description_event .... and other details if you need. 基本上,您应该创建一个包含以下内容的列的表:id_event,date_event,time_event,description_event ....和其他详细信息(如果需要)。

You would then query the database via a php script (see php pdo tutorials). 然后,您将通过php脚本查询数据库(请参阅php pdo教程)。 And ultimately you could add, store, modify events via javascript and ajax calls to the php script. 最终,您可以通过对php脚本的javascript和ajax调用来添加,存储,修改事件。 I personally use jQuery for that via the function getJSON. 我个人通过功能getJSON使用jQuery。

Good luck :) 祝好运 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM