Android Calendar class month's day-length setting problem
I am trying to define Jalali Calendar using java Calendar class. The problem comes when I set a jalali date like this:
Calendar jCal = Calendar.getInstance();
jCal.set(1390, 1, 31); //Current year in this calendar
Actually second month of year in Jalali calendar has 31 exactly days. Now when I call to get month and day, I get:
int day = jCal.get(Calendar.MONTH);
int month = jCal.get(Calendar.DAY_OF_MONTH);
gives: day = 3 & month = 2
Seems the date is re-adjusted according to Gregorian calendar. Similarly, I c开发者_运维技巧an not use getImeInMillis()
or 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);
again the result is day = 3 & month = 2
Can you please help me with this issue?
// 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;
}
The month
in the Calendar
is zero based. See Calendar.MONTH .
Edit: Maybe this implementation of the Jalali calendar can help you.
精彩评论