Egis
Egis

Reputation: 5141

Why java.util.Calendar class gives incorrect date on android?

I have Gregorian calendar instance using particular time zone. I set calendar's date to 1970-01-01. When I get year, month, day fields they are different from what I have set them to.

If I run this code on android device it prints "1969-11-31"

    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("America/Bahia_Banderas"));

    cal.clear();
    cal.set(1970, 0, 1);

    // Should print "1970-0-1" but prints "1969-11-31"
    android.util.Log.i("date", cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DAY_OF_MONTH));

However, running the same code on pure Java it works as expected.

    Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("America/Bahia_Banderas"));
    cal.clear();
    cal.set(1970, 0, 1);

    // Prints "1970-0-1" as expected
    System.out.println(cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DAY_OF_MONTH));

I've read about a bug in Calendar class which seemed to be related to my problem. I tried the workaround mentioned in the bug report but it didn't work for me. The strangest thing, however, is that the same class behaves differently on android and on pure Java. I want instance of calendar to return correct date after I set it. Do you know how to achieve that?

Upvotes: 2

Views: 1982

Answers (4)

laalto
laalto

Reputation: 152817

Calendar.clear() implementation is different in regular Java and Android.

In particular, the regular Java implementation sets isTimeSet to false. Android implementation does not.

When you create your Calendar, its time is set to the current time and isTimeSet is set to true. Calling clear() unsets/zeroes the field values in both implementations.

Your requested timezone is UTC+6 so 1970-01-01T00:00:00 is 1969-12-31T18:00:00 in your timezone. Since the isTimeSet flag is still set in Android, the time is not recomputed when requesting the datetime.

Upvotes: 3

Happy Cupz Cupz
Happy Cupz Cupz

Reputation: 181

remove cal.clear();

I've tried and it prints correctly.

edit: curiously. if you do this :

cal.clear(); 
cal.getTime();  // or any other methods that access the calendar object.
cal.set(1970, 0, 1);

it will print correctly.

I guess it has something to do with the explanation for .clear()

Clears the values of all the time fields, marking them all unset and assigning them all a value of zero. The actual field values will be determined the next time the fields are accessed.

Upvotes: 0

fanxl12
fanxl12

Reputation: 1

In android,you can use this way to get date: Calendar calendar = Calendar.getInstance(); calendar.set(1970, 0, 1);

System.out.println("year:"+calendar.get(Calendar.YEAR)+"--month:"+calendar.get(Calendar.MONTH)+"--day:"+calendar.get(Calendar.DAY_OF_MONTH));

The result is:year:1970 --month:0 --day:1

Upvotes: -1

SubinM
SubinM

Reputation: 384

Try this:

TimeZone MyTimezone = TimeZone.getDefault();
Calendar calendar = new GregorianCalendar(MyTimezone);
calendar.set(year, month, day, hour, minute, second);

String month_name=calendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());//Locale.US);
String day_name=calendar.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.getDefault());//Locale.US);

Upvotes: 0

Related Questions