我正在尝试使用Java Calendar类定义Jalali Calendar。 当我设置这样的jalali日期时会出现问题:
Calendar jCal = Calendar.getInstance();
jCal.set(1390, 1, 31); //Current year in this calendar
实际上,Jalali日历中的第二个月有31天。 现在,当我打电话来获得月和日时,我得到:
int day = jCal.get(Calendar.MONTH);
int month = jCal.get(Calendar.DAY_OF_MONTH);
给出: day = 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&月= 2
你可以帮我解决这个问题吗?
答案 0 :(得分:0)
但是你说那是Jalali日历? ofc Java认为你正在使用Gregorian。也许你需要使用类似的东西 this
或做自己的JalaliCalendar类
答案 1 :(得分:0)
// 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;
}
答案 2 :(得分:-1)
month
中的Calendar
基于零。请参阅Calendar.MONTH。
编辑:也许{JALli日历的this实施可以帮助您。