首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android Calendar类月份的全天设置问题

Android Calendar类月份的全天设置问题
EN

Stack Overflow用户
提问于 2011-07-27 03:36:43
回答 2查看 1K关注 0票数 0

我正在尝试使用java Calendar类来定义Jalali Calendar。当我像这样设置jalali日期时,问题就来了:

Calendar jCal = Calendar.getInstance();

jCal.set(1390, 1, 31); //Current year in this calendar

实际上,每年的第二个月恰好有31天。现在,当我调用获取月份和日期时,我得到:

代码语言:javascript
运行
复制
int day = jCal.get(Calendar.MONTH);
int month = jCal.get(Calendar.DAY_OF_MONTH);

给予:天=3&月=2

似乎日期是根据公历重新调整的。同样,我不能使用getImeInMillis()setTimeInMillis()

代码语言:javascript
运行
复制
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

你能帮我解决这个问题吗?

EN

回答 2

Stack Overflow用户

发布于 2016-09-23 11:50:02

代码语言:javascript
运行
复制
// 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;

}
票数 0
EN

Stack Overflow用户

发布于 2011-07-27 03:53:12

Calendar中的month是从零开始的。参见Calendar.MONTH

编辑:也许贾拉利日历的this实现可以帮助你。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6835515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档