Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the first week of a year

Using Java's Calendar or something else, let's say I set the date to 2012-10-26. I want this date to be the first day of a year. Is it possible to get the week number of a specific date -let's say it's 2012-12-10- assuming that the first week of the year starts from 2012-10-26?

I know my question sounds a little weird, but I could use any useful input.

Thanks.

--Details

I have a database table called WeeklySchedule. In this table I have columns starting from Week1 to Week52, and every column have some data about the days of that week. Unfortunately, Week1 is not really the first week of the year. I need to do my calculations assuming that Week1 starts from 2012-08-26.

like image 255
629 Avatar asked Oct 26 '12 04:10

629


People also ask

How do days of the week change from year to year?

It changes every year. It does so because its typical 365-day cycle is not evenly divisible by the number of days in the week: 365 ÷ 7 = 52, r 1. The unfortunate consequence of that one-day remainder is that the year typically begins and ends on the same weekday. So the next year must begin on the following weekday.

Can I change my calendar to start on Monday?

Go to Settings → General → Week. Tap the day on which you'd like the calendar week to start.

How do I change the first day of the week in Outlook calendar?

Click Home > Arrange > Month. Click the File tab. Click Options, and then click Calendar. Under Work time, for First day of week, select Monday.


2 Answers

Here's one approach using the Calendar API:

private static final Calendar START_CAL = createAugust26th2012Cal();

public int getWeekNumber(Calendar someCal) {
    int theWeek = someCal.get(Calendar.WEEK_OF_YEAR);
    int weekCount = 0;
    while (theWeek != START_CAL.get(Calendar.WEEK_OF_YEAR)) {
        someCal.add(Calendar.WEEK_OF_YEAR, -1);
        theWeek = someCal.get(Calendar.WEEK_OF_YEAR);
        weekCount++;
    }
    return weekCount;
}

Or you could get the diff (in millis) between two calendars and divide the result by the number of millis in a week:

If you use this approach, don't forget to take TimeZones and Daylight Saving Time into account.

I can't remember exactly what the code looks like to compensate for DST, but I think it's something like this: (I can't quite remember if you add the offset to or subtract the offset from the two operands)

private static final long ONE_WEEK_MILLIS = 1000 * 60 * 60 * 24 * 7;

public int getWeeksBetween(Calendar calOne, Calendar calTwo) {
    long millisOne = calOne.getTime().getTime();
    long millisTwo = calTwo.getTime().getTime();
    long offsetOne = calOne.getTimeZone().getOffset(millisOne);
    long offsetTwo = calTwo.getTimeZone().getOffset(millisTwo);
    long diff = (millisTwo - offsetTwo) - (millisOne + offsetOne );
    return diff / ONE_WEEK_MILLIS;
}
like image 160
jahroy Avatar answered Oct 01 '22 16:10

jahroy


I haven't ever seen a built-in function for this, only calls like Calendar.get(WEEK_OF_YEAR) or Joda's LocalDate.getWeekOfWeekyear(). java.util.Calendar (and Date) is often regarded as a bad library, compared to a library like Joda Time. This SO post has a good list of pros and cons.

Note that if your database table applies to year after year repeats across a year, you may run into trouble. Some years (71 years per 400, or about 1 every 5.6) have 53 weeks.

Given the above, you may want to settle for an approximation, such as counting the number of days between the two dates divided by 7 (all mod 52), which will slip by a small amount each year, or calculating (weekOfYear(date) - weekOfYear(startDate)) % 52, which may repeat a week around the beginning of the calendar year.

like image 22
Jeff Bowman Avatar answered Oct 01 '22 14:10

Jeff Bowman