Android java.util.Calendar - 时差 [英] Android java.util.Calendar - Time Difference

查看:127
本文介绍了Android java.util.Calendar - 时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作日历视图,以支持触摸交互。
所以我想建立新的自定义日历视图。
我试图在视图偏移量和实际日期值之间创建映射函数。

I want to make calendar view in order to support touch interaction. So I'd like to build new custom calendar view. I tried to make mapping function between view offset and real date value.

这是我的想法:
如果我可以计算周数由于基数日期(在我的情况下,1989-12-31),
很容易知道偏移量。 HEIGHT_FOR_WEEK * NUM_OF_WEEK是非常简单的计算
来知道确切的偏移。

Here is my idea: If I can compute the number of weeks since base date(in my case, 1989-12-31), it is easy to know offset. HEIGHT_FOR_WEEK * NUM_OF_WEEK is very simple computation to know exact offset.

我的问题是这样:
首先我从基数日期获得了毫秒值。并且我将毫秒设置为
另一个日历对象。我期望从该对象的同一日期。但实际上
是不同的日期。

My problem is this: First I got milliseconds value from base date. And I set the milliseconds to another calendar object. I expected same date from that object. But actually it was different date.

mBaseDateInMillis = mBaseDate.getTimeInMillis();
mAnotherDate.setTimeInMillis(mBaseDateInMillis);
/* I expect mBaseDate == mAnotherDate.
 * but it was different.
 */ 

这是我的代码:

public class CalendarCoordinate {
    public static final long ONEWEEK_IN_MILLISECONDS = 60 * 60 * 24 * 7 * 1000;
    public Calendar mBaseDate = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
    public long mBaseDateInMillis = 0;
    public Calendar mDate = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
    public int  mWeekHeight = 30;

    /**
     * CTOR
     */
    public CalendarCoordinate() {
        /* Base date is 1989-12-31 0, 0, 0
         * It was Sunday and offset 0 will be mapped onto this day.
         */
        mBaseDate.set(Calendar.MILLISECOND, 0);
        mBaseDate.set(1989, 12, 31, 0, 0, 0);
        mBaseDateInMillis = mBaseDate.getTimeInMillis();
        Log.v(TAG, "BaseDate:" + mBaseDate.toString());
    }

    /**
     * Compute DATE from Y-Offset
     * @param yOffset
     * @return
     */
     public Calendar dateFromYOffset(int yOffset) {
        long nthWeeks = yOffset / mWeekHeight;
        long millsSinceBaseDate = nthWeeks * ONEWEEK_IN_MILLISECONDS;
        mDate.clear();
        mDate.set(Calendar.MILLISECOND, 0);
        mDate.setTimeInMillis(mBaseDateInMillis + millsSinceBaseDate);

        /* We SHOULD call to update mDate internal data structure. 
         * Java is really strange for this thing
         **/
        mDate.getTimeInMillis();
        return mDate;
    }

    /**
     * Compute Y-Offset from DATE
     * @param date
     * @return
     */
    public long yOffsetFromDate(Calendar cal) {
        long mills = cal.getTimeInMillis();
        long nthWeeks = (mills - mBaseDateInMillis)/ONEWEEK_IN_MILLISECONDS;
        return nthWeeks * mWeekHeight;
    }
}

有人可以帮助我吗?我不是一个好的Java程序员。

Anybody can help me? I'm not a good Java programmer.

推荐答案

此语句让我感到困惑:

/* I expect mBaseDate == mAnotherDate.
 * but it was different.
 */

您是否正在通过比较来检查平等:
if(mBaseDate == mAnotherDate){System.out.println(它们相同); }

Are you actually trying to check for equality by doing the comparison: if (mBaseDate == mAnotherDate) { System.out.println("They are the same"); }

如果是这样,你的问题是你误解了==操作符在Java中的工作原理。它比较引用,而不是比较底层对象数据,并且因为它们是始终为false的不同对象(具有相同的值)。有关更多详细信息,请参阅比较运算符的Java笔记

If so, your issue is that you are misunderstanding how the "==" operator works in Java. It compares references, rather than comparing the underlying object data, and since these are different objects (with the same values) that will always be false. For a lot more details, see the Java Notes on comparison operators.

此外,这些行对我来说真的很可疑:

Also, these lines look really suspicious to me:

/* We SHOULD call to update mDate internal data structure. 
* Java is really strange for this thing
**/
mDate.getTimeInMillis();

如果Android有一个bug需要你这样做,我真的很惊讶,但我想有什么是可能。没有这个电话,你有什么问题?

I would really be surprised if Android had a bug requiring you to do this, but I guess anything is possible. What kind of problems do you have without this call?

这篇关于Android java.util.Calendar - 时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆