Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong "week of year" in Android

The number of "week of year" returned from a Date is wrong.

This is my code:

Calendar c = Calendar.getInstance();
c.setTime(my_date);
int num_week = c.get(Calendar.WEEK_OF_YEAR);

If my_date (type Date) is 01/01/2011, I supposed that "week of year" is 1. But it returned 52.

I try to test with these methods but I don't obtain anything:

c.setFirstDayOfWeek(6);
c.setMinimalDaysInFirstWeek(1)

If It's interesting, I'm from Spain, and our week begin on Monday.

Have I to do anything for obtain right results?

Thanks!

like image 915
Josue Avatar asked Oct 11 '22 02:10

Josue


1 Answers

This may be Android/Harmony-specific. For example, this works for me with desktop Java:

import java.util.*;

public class Test {
    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(2011, 0, 1, 0, 0, 0);
        System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // Prints 52
        calendar.setMinimalDaysInFirstWeek(1);
        System.out.println(calendar.get(Calendar.WEEK_OF_YEAR)); // Prints 1
    }
}

Can you confirm that the exact same code (modulo logging options) logs 52 twice on Android?

like image 175
Jon Skeet Avatar answered Oct 15 '22 16:10

Jon Skeet