2017-08-28 19 views
1

我想告訴賈拉利日曆和回曆的同時賈利利壓延機,但我不知道如何賈拉利天,回曆天相匹配。我搜索並找到了下面的源代碼。它顯示日曆日曆,但我不知道如何匹配兩個日曆。 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); 

} 


} 
+0

閏年規則顯然是非常相似的陽曆,因此不正確的。當然,Jalali(波斯語)和Hijri日曆的月份並不完全相關(Jalali嚴格按照春分,而Hijri是嚴格的月球)。您還應該考慮Hijri日曆有多種變體(您是否想要沙特阿拉伯或其他什麼?)。請提供更多關於你想要的細節。兩個日曆之間唯一的共同點就是自某個時代以來的天數(允許進行數學轉換)。 –

+0

你也可以使用我的庫[Time4J](https://github.com/MenoData/Time4J)(使用ui-module)並在用戶界面中嵌入兩個圖形JavaFX日曆,一個用於Persian,一個用於Hijri,然後同步兩個日曆,也看到了[javadoc的(http://time4j.net/javadoc-en/net/time4j/ui/javafx/CalendarPicker.html) –

+0

我知道賈拉利閏年是回曆(沙特阿拉伯不同)和該代碼工作正常波斯日曆,但想在這個環節http://www.time.ir/我想同步傑拉裏天,回曆天在同一時間日曆,因爲在波斯曆法一些假期是回曆節假日和我想在我的程序中以不同的顏色顯示它們。 – Muskan

回答

0

一個非常簡單的方法:

  1. 採取兩種曆法
  2. 之間共同發展的時代/查找算法,日曆轉換日期/從那個時代之間的天數。從日曆型
  3. 轉換日期爲劃時代的三角洲和劃時代增量到日曆類型B.

我實現了一個簡單的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 

注:

  • 賈拉利日曆不再使用。您可能想要使用Solar Hijri日曆。
  • Hijri日曆不是表格。你可能意味着你的賈拉利日曆伊斯蘭民事Clanndar