我想同时显示贾拉利日历和回历日历,但我不知道如何将贾拉利日与回历日匹配。我搜索了一下,找到了下面的源代码。它显示 jalali 日历,但我不知道如何匹配两个日历。回历月份没有任何特定模式。
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);
}
}
最佳答案
一个非常简单的方法:
- 取两个日历之间的共同纪元
- 开发/寻找算法,将日历日期与该纪元之间的天数相互转换。
- 将日期从日历类型 A 转换为纪元增量,并将纪元增量转换为日历类型 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
注释:
- Jalali 日历不再使用。您可能想使用阳历回历。
- 回历不是表格。您可能指的是伊斯兰公民部落
关于java - 如何将贾拉利日历与回历日历相匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45915854/