2017-08-28 9 views
1

jalaliカレンダーとhijriカレンダーを同時に表示したいが、jalali日とhijri日をどのように一致させるかわからない。私は以下のソースコードを検索して見つけました。それは、jalaliのカレンダーを示していますが、私は2つのカレンダーを一致させる方法を知らない。 Hijriカレンダーは何ヶ月間も特定のパターンを持っていません。jaliliカレンダーと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カレンダーの閏年のルールは明らかにグレゴリオンと非常によく似ており、したがって正しくありません。もちろん、Jalali(ペルシャ語)とHijriカレンダーの月は、お互いに関連していません(Jalaliは厳密には春分に続いて太陽があり、Hijriは厳密に月です)。また、ヒジリのカレンダーにはいくつかのバリエーションがあることを考慮する必要があります(サウジアラビアやその他のものが必要ですか?)。あなたが何を望むかについての詳細を教えてください。両方のカレンダー間の唯一の共通点は、いくつかのエポック(数学的変換を可能にする)からの日数です。 –

+0

また、私のライブラリ[Time4J](https://github.com/MenoData/Time4J)(ui-moduleを使用)を使用し、2つのグラフィカルなJavaFXカレンダーをペルシャ語用と1つのユーザーインターフェイスのHijri用に組み込み両方のカレンダーを同期させてください。[javadoc](http://time4j.net/javadoc-en/net/time4j/ui/javafx/CalendarPicker.html) –

+0

を参照してください。私はJalaliの閏年がHijri(Saudi-ArabiaこのコードはPersianのカレンダーでうまく動作しますが、このリンクのカレンダーのようにhttp://www.time.ir/ペルシャのカレンダーの一部はHijriの休日であるため、Jalaliの日とHijriの日を同時に同期したい自分のプログラムで色を変えて見せたい。 – Muskan

答えて

0

非常に単純なアプローチ:

  1. はそのエポックの間の日数へ/からカレンダーの日付を変換するためのアルゴリズムを探す/開発2つのカレンダー
  2. 間の共通エポックしてください。タイプBをカレンダーにエポック - デルタとエポック - デルタへ
  3. カレンダータイプAから
  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 

注:

  • はジャラリ暦はもう使用されていません。あなたはたぶんSolar Hijri Calendarを使いたいと思うでしょう。
  • Hijriカレンダーは表形式ではありません。あなたはおそらくイスラム民衆のクランダを意味します
関連する問題