Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble getting Calendar.WEEK_OF_YEAR

I've been trying to get this to work for days and I'm tearing my hair out. I think I just need another pair of eyes to help me out.

I have "Entries" stored in a database, each with a Unix timestamp on the second they were entered. I have a system where the user can select any date and view the entries stored on the same week. The problem arises when I let the user set a custom first day of the week with Calendar.setFirstDayOfWeek(). I can never get a correct return value from the database with the first day of the week set to something other than Sunday (for this example, I'm using Tuesday).

public void getEntries() {

    /* Make sure temp Calendar has same first day */
    temp = Calendar.getInstance();
    temp.setFirstDayOfWeek(date.getFirstDayOfWeek());
    /* Common date ranges in unix time */
    long oneday = 86400000L;
    long oneweek = 604800000L;
    long onemonth = 2628000000L;
    long oneyear = 31536000000L;
    /* The custom date in unix time. For example, the user may have selected Dec. 1, 2012 */
    long set_time = date.getTimeInMillis();
    entries.clear();
    dba.open();

    /* This query gets entries within 1 week and 1 day of the user-set date just to be safe */
    Cursor cc = dba.query("Date < " +(set_time+oneweek+oneday)+ " AND Date > " +(set_time-oneday-oneweek));
            if(cc.moveToFirst()) do {
                /* Set temp calendar to the timestamp of the entry in database */
                long unix = cc.getLong(1);
                temp.setTimeInMillis(unix);
                System.out.println("Temp calendar set to "+temp.getTime().toGMTString()+ " fdow("+temp.getFirstDayOfWeek()+") week("+temp.get(Calendar.WEEK_OF_YEAR)+"), user calendar set to "+date.getTime().toGMTString()+" fdow("+date.getFirstDayOfWeek()+") week("+date.get(Calendar.WEEK_OF_YEAR)+")");
                if(temp.get(Calendar.WEEK_OF_YEAR) == date.get(Calendar.WEEK_OF_YEAR)
                && temp.get(Calendar.YEAR) == date.get(Calendar.YEAR) )
                    /* Entry object is instantiated and added to ArrayList if WEEK_OF_YEAR in temp calendar matches WEEK_OF_YEAR in global calendar */
                    entries.add(createEntryFromCursor(cc));
            } while(cc.moveToNext());

}

In my tests, I know for a fact there is an entry at Nov. 30, 2012. With a first day of week set to Tuesday (and the entry was on Friday), this should return 1 result if the user sets the date as 1 Dec, and no result if the user sets 26 Nov. (a Monday). However, I get 1 result for both. What's wrong?!

Edit

I still really need help! I've updated the above with my current code. I'm still getting odd results. For example, with a first day of week as Monday, I'm getting this from System.out:

12-09 11:46:38.465: I/System.out(15130): Temp calendar set to 9 Dec 2012 19:29:59 GMT fdow(2) week(49), user calendar set to 9 Dec 2012 14:51:59 GMT fdow(2) week(50)

This is saying that 9 Dec, 2012 occurs in 2 different WEEK_OF_YEARs (49/50) even though both calendars have the first day of week equal to 2. Umm.. what??

Solution

I ditched getting the WEEK_OF_YEAR and followed the suggestion to get the start and end Dates for the week.

            // get rollback amt
            temp.setTimeInMillis(set_time);
            int rollback = 0;
            int dayofweek = date.get(Calendar.DAY_OF_WEEK);
            while(dayofweek != temp.getFirstDayOfWeek()) {
                dayofweek--;
                if(dayofweek == 0) dayofweek = 7;
                rollback--;
            }
            int rollfwd = rollback + 6;

            // get start bound
            temp.setTimeInMillis(set_time);
            temp.roll(Calendar.DAY_OF_YEAR, rollback);
            temp.set(Calendar.HOUR_OF_DAY, 0);
            temp.set(Calendar.MINUTE, 1);
            Date start = temp.getTime();
            // get end bound
            temp.setTimeInMillis(set_time);
            temp.roll(Calendar.DAY_OF_YEAR, rollfwd);
            temp.set(Calendar.HOUR_OF_DAY, 23);
            temp.set(Calendar.MINUTE, 59);
            Date end = temp.getTime();

Then I can check if the Date of the Entry is between those:

if(entrydate.after(start) && entrydate.before(end))
like image 789
Snailer Avatar asked Dec 01 '12 17:12

Snailer


1 Answers

Can you use SimpleDateFormat in android to get week_of_year and validate the conditions for querying the user selected date.

Step 1: Get user choice for selecting the week. eg:27

Step 2: get start of the week and ending of the week. start of 27th week to end of 27th week.

Step 3: Use this two boundary conditions to filter your result set.

Hope this will help you to resolve your issue.

like image 109
Ajay Kumar Meher Avatar answered Sep 23 '22 00:09

Ajay Kumar Meher