6

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!

wattostudios
  • 8,666
  • 13
  • 43
  • 57
Josue
  • 725
  • 2
  • 10
  • 22
  • possible duplicate of [Why dec 31 2010 returns 1 as week of year?](http://stackoverflow.com/questions/4608470/why-dec-31-2010-returns-1-as-week-of-year) – Metro Smurf Sep 01 '11 at 16:16
  • The first and last week of the year are dependent on locale - see link above for a duplicate question and explanation. – Metro Smurf Sep 01 '11 at 16:18
  • @Metro: But setting the minimal number of days of the first week to 1 *should* fix it regardless. – Jon Skeet Sep 01 '11 at 16:20

2 Answers2

4

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?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • What happens with: `Calendar.getInstance(Locale.SPAIN)`? – Metro Smurf Sep 01 '11 at 16:23
  • @Metro: With `new Locale("es")` I get 1 both times. With `new Locale("es", "ES")` I get 52 and 1. – Jon Skeet Sep 01 '11 at 16:25
  • Interesting. I'm curious to see if this is Android specific. – Metro Smurf Sep 01 '11 at 16:28
  • Got curious and ran your sample code against every available locale on Android 2.3. All values returned incorrectly with 52/52. Then ran against Android 3.2. All locales except "fa_x" and most of "ar_x" again returned incorrectly with 52/52 (fa_x, ar_x returned 1/1). – Metro Smurf Sep 01 '11 at 18:44
  • @Metro: Thanks for the testing. Curious. Have you tried the Apache Harmony project on a desktop? – Jon Skeet Sep 01 '11 at 18:45
  • I am completely new with the Java platform; just trying to make a cross-over to developing Android apps. The Apache Harmony project is above my pay-grade. – Metro Smurf Sep 01 '11 at 19:47
  • @Metro: Fair enough - thanks for the Android testing anyway :) – Jon Skeet Sep 01 '11 at 20:10
  • Thank you! Now It seems it work. I use: Calendar c = Calendar.getInstance(new Locale("es", "ES")); c.setMinimalDaysInFirstWeek(2); I test with different values of setMinimalDaysInFirstWeek, and the results for 1/1/2011 were: - Value 1: 52 - Value 2: 1 - Value 3: 53 - Value 4: 52 I test with 2 and It works. The only problem is that some days of last week of the year (for example 31/12) it detects as week 1, but I show a 52 to the user. and – Josue Sep 02 '11 at 09:37
0

Here you can view the reference by oracle

https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html

And I have placed a quick solution to find the week count of current day. You can alter and optimize as your way. Also set according to your convenient GMT value

public static int getWeeksOfMonth() {

    DATESTATUS = false;
    VALUESTATUS = false;
    int weekCount;
    WEEK_OF_MONTH= -1;

    // get the supported ids for GMT+04:00 (Pacific Standard Time)
    String[] ids = getAvailableIDs(4 * 60 * 60 * 1000);
    // if no ids were returned, something is wrong. get out.
    if (ids.length == 0)
        return WEEK_OF_MONTH;

    // create a Pacific Standard Time time zone
    SimpleTimeZone pdt = new SimpleTimeZone(4 * 60 * 60 * 1000, ids[0]);

    // create a GregorianCalendar with the Pacific Daylight time zone
    // and the current date and time
    Calendar calendar = new GregorianCalendar(pdt);
    Date trialTime = new Date();
    calendar.setTime(trialTime);

    weekCount = calendar.get(Calendar.WEEK_OF_YEAR);

    return recursiveWeekCountCheck(calendar, weekCount);
}

private static int recursiveWeekCountCheck(Calendar calendar, int weekCount) {
    if (calendar.get(Calendar.MONTH) == Calendar.DECEMBER && weekCount == 1) {
        DATESTATUS = true;
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        weekCount = calendar.get(Calendar.WEEK_OF_YEAR);
        recursiveWeekCountCheck(calendar, weekCount);
    }
    if (!VALUESTATUS){
        VALUESTATUS = true;
        if (DATESTATUS) {
            weekCount++;
            WEEK_OF_MONTH = weekCount;
        } else {
            WEEK_OF_MONTH = weekCount;
        }
    }
    return WEEK_OF_MONTH;
}

At the end just call the method getWeeksOfMonth();

Cyril David
  • 465
  • 1
  • 4
  • 11