我怎样才能将jalili日历与hijri日历相匹配

时间:2017-08-28 09:30:10

标签: java calendar

我想同时展示jalali日历和hijri日历,但我不知道如何将jalali日与hijri日相匹配。我搜索并找到了下面的源代码。它显示了jalali日历,但我不知道如何匹配两个日历。几个月来,Hijri日历没有任何特定的模式。

import javax.swing.JOptionPane;

 public class TestCal {
    /** Main method */

public static void main(String[] args) {

    // Prompt the user to enter year
    String yearString = JOptionPane.showInputDialog(

            "Lotfan Sale morede nazar ra vared konid (Masalan 1390):");



    // Convert string into integer
    int year = Integer.parseInt(yearString);



    // Prompt the user to enter month
    String monthString = JOptionPane.showInputDialog(

            "Lotfan mahe khod ra niz az miane adade 1 ta 12 bargozinid:");



    // Convert string into integer
    int month = Integer.parseInt(monthString);



    // Print calendar for the month of the year
    printMonth(year, month);

}



/** Print the calendar for a month in a year */

static void printMonth(int year, int month) {

    // Print the headings of the calendar
    printMonthTitle(year, month);



    // Print the body of the calendar
    printMonthBody(year, month);

}



/** Print the month title, e.g., May, 1999 */

static void printMonthTitle(int year, int month) {

    System.out.println("         " + getMonthName(month)

            + " " + year);

    System.out.println("-----------------------------");

    System.out.println(" Sun Mon Tue Wed Thu Fri Sat");

}



/** Get the English name for the month */

public static String getMonthName(int month) {

    String monthName = null;

    switch (month) {

        case 1: monthName = "Farvardin"; break;

        case 2: monthName = "Ordibehesht"; break;

        case 3: monthName = "Khordad"; break;

        case 4: monthName = "Tir"; break;

        case 5: monthName = "Mordad"; break;

        case 6: monthName = "Shahrivar"; break;

        case 7: monthName = "Mehr"; break;

        case 8: monthName = "Aban"; break;

        case 9: monthName = "Azar"; break;

        case 10: monthName = "Dey"; break;

        case 11: monthName = "Bahman"; break;

        case 12: monthName = "Esfand";

    }



    return monthName;

}



/** Print month body */

public static void printMonthBody(int year, int month) {

    // Get start day of the week for the first date in the month
    int startDay = getStartDay(year, month);



    // Get number of days in the month
    int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);



    // Pad space before the first day of the month
    int i = 0;

    for (i = 0; i < startDay; i++)

        System.out.print("    ");



    for (i = 1; i <= numberOfDaysInMonth; i++) {

        if (i < 10)

            System.out.print("   " + i);

        else

            System.out.print("  " + i);



        if ((i + startDay) % 7 == 0)

            System.out.println();

    }



    System.out.println();

}



/** Get the start day of the first day in a month */

public static int getStartDay(int year, int month) {

    // Get total number of days since 1/1/1800
    int startDay1300 = 1;

    int totalNumberOfDays = getTotalNumberOfDays(year, month);



    // Return the start day
    return (totalNumberOfDays + startDay1300) % 7;

}



/** Get the total number of days since Jan 1, 1800 */

static int getTotalNumberOfDays(int year, int month) {

    int total = 0;



    // Get the total days from 1800 to year - 1
    for (int i = 1300; i < year; i++)

        if (isLeapYear(i))

            total = total + 366;

        else

            total = total + 365;



    // Add days from Jan to the month prior to the calendar month
    for (int i = 1; i < month; i++)

        total = total + getNumberOfDaysInMonth(year, i);



    return total;

}



/** Get the number of days in a month */

public static int getNumberOfDaysInMonth(int year, int month) {

    if (month <= 6 && month > 0)

        return 31;



    else if (month < 12 && month > 6)

        return 30;



    if (month == 12) return isLeapYear(year) ? 30 : 29;



    return 0; // If month is incorrect
}



/** Determine if it is a leap year */

public static boolean isLeapYear(int year) {

    return year % 400 == 0 || (year % 4 == 3 && year % 100 != 0);

}


}

1 个答案:

答案 0 :(得分:0)

一种非常简单的方法:

  1. 在两个日历之间采用共同的时代
  2. 开发/查找算法,将日历日期转换为该时期之间的天数。
  3. 将日期类型A的日期转换为纪元 - 增量和纪元 - 增量到日历类型B.
  4. 我已经实现了一个简单的C库来在各种日历中的日期之间进行转换。代码可用here。您可以提取算法并使用您自己选择的语言实现它们。如果您使用的是C / C ++,可以简单地说:

    #include <calendars/cl-calendar.h>
    int16_t year;
    uint8_t month;
    uint16_t day;
    convert_date(CAL_ISLAMIC_CIVIL, CAL_SOLAR_HIJRI,
                 1396, 7, 11, &year, &month, &day);
    printf("1396/07/11 AP is: %d/%d/%d Hijri", year, month, day);
    

    将打印:

    $ 1396/07/11 AP is: 1439/1/12 Hijri
    

    注意:

    • Jalali日历不再使用了。您可能想要使用Solar Hijri Calendar。
    • Hijri日历不是表格。你可能意味着伊斯兰民用Clanndar