开发者

Why does calendar.isSet(field) change to true when calling many of the other methods?

I have a project where the isSet method of a Calendar would be very useful, but between clearing a field and reading the isSet flag for that field I need to call another method of the calendar that is affected by this issue.

Here's a sample to demonstrate what is going on:

Calendar cal = Calendar.getInstance();
Log.d(TAG, Boolean.toString(cal.isSet(Calendar.SECOND))); // true
cal.clear(Calendar.SECOND);
Log.d(TAG, Boolean.toString(cal.isSet(Calendar.SECOND))); // false
cal.getTimeInMillis();
// These other methods I've tried, and I'm sure many more, have the same affect on isSet:
// before(calendar), compareTo(calendar), get(field), add(field, int), set(field, int)
// Note: 'field' in above comment line refers to a field other than Calendar.SECOND
Log.d(TAG, Boolean.toString(cal.isSet(Calendar.SECOND))); // true

I've looked at the source for the Calendar class and I don't see what's causing this. Does anyone know why this is happening? Do I need to track the set fields separately?

Update:

It appears this issue may be Android-specific or perhaps a specific version of Java since a couple answerers said it works for them in standard Java. Just to be clear, the class I'm using is still java.util.Calendar. In case the info might help, I am building against the Android 2.2 platform (API 8).

Update 2:

According to someone in #android-dev on FreeNode and my subsequent research on Wikipedia, this is probably because Dalvik uses a subset of Harmony (a Java implementation) for its class library instead of Java. However, that then gets me wondering why Harmony changes the isSet behavior. Needless to say, this issue sounds like it's开发者_Python百科 way too far upstream for there to be a fix in the Android SDK any time soon, so I'll need to settle for a workaround.

I'll leave the question open since it hasn't been properly answered yet. Is it bug in Harmony as I suspect or intentional design? If intentional, what is the purpose and is there a way to use it as I'm wanting to use it?

Update 3:

I've posted the issue to the Android issue tracker, now to wait and see if anything happens from it. Please star it if this issue is a problem for you as well. I'm hoping Android has its own fork of Harmony and doesn't need to wait for the fix upstream.

https://code.google.com/p/android/issues/detail?id=16826

Also removed the java tag since this is clearly not a Java issue.


Works as expected for me


Working as expected for me..

Here is the code I have used

import java.util.Calendar;

public class Test {

/**
 * @param args
 */
public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();
    System.out.println(Boolean.toString(cal.isSet(Calendar.SECOND)));
    cal.clear(Calendar.SECOND);
    System.out.println(Boolean.toString(cal.isSet(Calendar.SECOND)));
    cal.getTimeInMillis();
    System.out.println(Boolean.toString(cal.isSet(Calendar.SECOND)));
}

}

And here is here is the output in console

true

false

false

Thanks,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜