Skip to content Skip to sidebar Skip to footer

Android Calendar: First And Last Day Of Week -> Zero Month

I want to get the date of the first monday of the current week and the first friday of the current week. I tried it this way: Calendar calendarFirstDay = Calendar.getInstance(L

Solution 1:

From the JavaDocs:

Field number for get and set indicating the month. This is a calendar-specific value. The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.

Thus the first month (January) has a MONTH value of 0, and not 1 (as you'd expect).

There's a much better solution though: use a DateFormat or SimpleDateFormat to format dates as text. That way, you simply don't have to worry about this and let the DateFormat take care of it. For example:

DateFormatmyFormat=newSimpleDateFormat("dd.MM.yyyy");
MyTextView.setText(
    myFormat.format(calendarFirstDay.getTime()) + " - " +
    myFormat.format(calendarLastDay.getTime())
);

Solution 2:

As stated in

http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#set(int,%20int,%20int)

if you use this method, January will be 0 and Decmember will be 11, so you should just add 1 to your month.

EDIT: You are using the get method but it's probably 0-based, too.

Post a Comment for "Android Calendar: First And Last Day Of Week -> Zero Month"