How are week numbers determined (specifically the first week of the year)

2
I'm currently working on an application where I have to report on dates, and the application is used in different countries. I know that week nr's could vary per country. Because of these differences I'm trying to understand the exact logic of the week nr's in Mendix. So far I've only found some contradicting information. The statement below is copied from the Java Calendar documentation page. The behavior in Mendix is different than this. Values calculated for the WEEK_OF_YEAR field range from 1 to 53. Week 1 for a year is the earliest seven day period starting on getFirstDayOfWeek() that contains at least getMinimalDaysInFirstWeek() days from that year. It thus depends on the values of getMinimalDaysInFirstWeek(), getFirstDayOfWeek(), and the day of the week of January 1. Weeks between week 1 of one year and week 1 of the following year are numbered sequentially from 2 to 52 or 53 (as needed). This particular answer was also given in forum post: https://forum.mendix.com/questions/5783/First-Week-of-Year In another forum post a reference is posted to a page that also returns different results than what we have found so far. https://forum.mendix.com/questions/947/Weeknumbers-using-formatDateTime--in-Mendix-2461--differs-depending-on-localize-settings I see the following results, using the following expression: formatDateTimeUTC($Order/OrderDate, 'ww') Locale US: Sat 12/27/2015 - Week 52 Locale US: Sun 12/28/2015 - Week 1 Locale US: Fri 1/1/2016 - Week 1 Locale US: Sun 1/3/2016 - Week 2 Locale US: Mon 1/4/2016 - Week 2 Locale NL: Sat 12/27/2015 - Week 52 Locale NL: Sun 12/28/2015 - Week 53 Locale NL: Fri 1/1/2016 - Week 53 Locale NL: Sun 1/3/2016 - Week 53 Locale NL: Mon 1/4/2016 - Week 1 The most important thing for me is to understand how the platform decides to use a certain week nr. In my application the week nr is a (stored) critical part of my system and needs to be predictable, and I would like to understand the impact languages have on the dates in scheduled events and microflow buttons. Would somebody be able to reference what the platform actually uses to localize the dates and format them?
asked
1 answers
2

Jasper,

The platform adheres to the java method with localized dates. The differences that you see are due to the fact that week 1 is determined differently in Europe and the U.S. In the U.S. the the 1. Week of Year is the week where the 1. January belongs to. -- So if 1. Januar is a Saturday, then the Friday before (31. Dec) belongs the same week, and in this case this day belongs to the 1. Week of the next year. In the EU the first day of the week is Monday and the according to ISO 8601 standard followed: the first Week* of Year is the first week with 4 or more days in the new year. Try your dates in the code below to test that the platform adheres to the java way of calculating the weeks:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class LocaizedDates {
    public static void main(String[] args) throws ParseException {

        DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        Date lastDec2010 = sdf.parse("28/12/2015");

        Calendar calUs = Calendar.getInstance(Locale.US);       
        calUs.setTime(lastDec2010);

        Calendar calDe = Calendar.getInstance(Locale.GERMAN);       
        calDe.setTime(lastDec2010);

        System.out.println( "us: " + calUs.get( Calendar.WEEK_OF_YEAR ) ); 
        System.out.println( "de: " + calDe.get( Calendar.WEEK_OF_YEAR ) );
    }
}
answered