我正在尝试使用java Calendar类来定义Jalali Calendar。当我像这样设置jalali日期时,问题就来了:
Calendar jCal = Calendar.getInstance();
jCal.set(1390, 1, 31); //Current year in this calendar
实际上,每年的第二个月恰好有31天。现在,当我调用获取月份和日期时,我得到:
int day = jCal.get(Calendar.MONTH);
int month = jCal.get(Calendar.DAY_OF_MONTH);给予:天=3&月=2
似乎日期是根据公历重新调整的。同样,我不能使用getImeInMillis()或setTimeInMillis()
Calendar testCal = Calendar.getInstance();
testCal.set(1380, 1, 28);
long millis = testCal.getTimeInMillis();
testCal.setTimeInMillis(millis + 3 * 24 * 3600 * 1000);
int day = jCal.get(Calendar.MONTH);
int month = jCal.get(Calendar.DAY_OF_MONTH);同样,结果是day =3& month =2
你能帮我解决这个问题吗?
发布于 2016-09-23 11:50:02
// 00:00:00 UTC (Gregorian) Julian day 0,
// 0 milliseconds since 1970-01-01
public static final long MILLIS_JULIAN_EPOCH = -210866803200000L;
// Milliseconds of a day calculated by 24L(hours) * 60L(minutes) *
// 60L(seconds) * 1000L(mili);
public static final long MILLIS_OF_A_DAY = 86400000L;
/**
* The JDN of 1 Farvardin 1; Equivalent to March 19, 622 A.D.
*/
public static final long PERSIAN_EPOCH = 1948321;
private int persianYear;
private int persianMonth;
private int persianDay;
public static long persianToJulian(long year, int month, int day) {
return 365L * ((ceil(year - 474L, 2820D) + 474L) - 1L) + ((long) Math.floor((682L * (ceil(year - 474L, 2820D) + 474L) - 110L) / 2816D)) + (PERSIAN_EPOCH - 1L) + 1029983L
* ((long) Math.floor((year - 474L) / 2820D)) + (month < 7 ? 31 * month : 30 * month + 6) + day;
}
public void setPersianDate(int persianYear, int persianMonth, int persianDay) {
this.persianYear = persianYear;
this.persianMonth = persianMonth;
this.persianDay = persianDay;
setTimeInMillis(convertToMilis(persianToJulian(this.persianYear > 0 ? this.persianYear : this.persianYear + 1, this.persianMonth - 1, this.persianDay)));
}
public static long julianToPersian(long julianDate) {
long persianEpochInJulian = julianDate - persianToJulian(475L, 0, 1);
long cyear = ceil(persianEpochInJulian, 1029983D);
long ycycle = cyear != 1029982L ? ((long) Math.floor((2816D * (double) cyear + 1031337D) / 1028522D)) : 2820L;
long year = 474L + 2820L * ((long) Math.floor(persianEpochInJulian / 1029983D)) + ycycle;
long aux = (1L + julianDate) - persianToJulian(year, 0, 1);
int month = (int) (aux > 186L ? Math.ceil((double) (aux - 6L) / 30D) - 1 : Math.ceil((double) aux / 31D) - 1);
int day = (int) (julianDate - (persianToJulian(year, month, 1) - 1L));
return (year << 16) | (month << 8) | day;
}
protected void calculatePersianDate() {
long julianDate = ((long) Math.floor((getTimeInMillis() - MILLIS_JULIAN_EPOCH)) / MILLIS_OF_A_DAY);
long PersianRowDate = julianToPersian(julianDate);
long year = PersianRowDate >> 16;
int month = (int) (PersianRowDate & 0xff00) >> 8;
int day = (int) (PersianRowDate & 0xff);
this.persianYear = (int) (year > 0 ? year : year - 1);
this.persianMonth = month;
this.persianDay = day;
}发布于 2011-07-27 03:53:12
Calendar中的month是从零开始的。参见Calendar.MONTH。
编辑:也许贾拉利日历的this实现可以帮助你。
https://stackoverflow.com/questions/6835515
复制相似问题