Get week number from a Date object

928 views
Skip to first unread message

Glimpse

unread,
Aug 5, 2010, 11:15:06 AM8/5/10
to Google Web Toolkit
Hello all,

I'd like to know if there is an easy way to get the week number from a
Date object. In pure Java, the Calendar class does the trick via a
call to get(Calendar.WEEK_OF_YEAR); but the Calendar class is not
available in GWT.

Any idea?

ctasada

unread,
Aug 5, 2010, 5:13:22 PM8/5/10
to Google Web Toolkit
AFAIK the only way is calculating it yourself

public static int calendarWeekIso(final Date inputDate) {

int thursdayDay = 4 + firstDayOfWeek;

Date thisThursday = new Date(inputDate.getYear(),
inputDate.getMonth(),
inputDate.getDate() - weekday(inputDate) + thursdayDay);

Date firstThursdayOfYear = new Date(thisThursday.getYear(), 0, 1);

while (weekday(firstThursdayOfYear) != thursdayDay) {
firstThursdayOfYear.setDate(firstThursdayOfYear.getDate() + 1);
}

Date firstMondayOfYear = new Date(firstThursdayOfYear.getYear(), 0,
firstThursdayOfYear.getDate() - 3);

Long cw = (thisThursday.getTime() - firstMondayOfYear.getTime())
/ ONE_DAY / DAYS_IN_A_WEEK + 1;

return cw.intValue();

Glimpse

unread,
Aug 6, 2010, 4:48:28 AM8/6/10
to Google Web Toolkit
Thank you.

Maybe I made a mistake when coding it with your algorithm, but it
seems that it does not always work.
As a reference, I'm using this website (in french):
http://www.calendrier-365.fr/calendrier-2010.html

And I found this script to work well: http://syn.ac/tech/19/get-the-weeknumber-with-javascript/
It is easy to transform into Java/GWT (but with all the deprecation
warnings around the Date object).

Stefano Ciccarelli

unread,
Aug 6, 2010, 5:32:12 AM8/6/10
to google-we...@googlegroups.com
If you need the ISO week this is what I'm using in GWT:

    private static final int ISO_THURSDAY = 4;
    private static final int MAX_DAY_OF_WEEK = 6;
    private static final int DAYS_IN_WEEK = 7;
    private static final long MILLIS_DAY = 86400000;

    @SuppressWarnings("deprecation")
    public int getWeek() {
        final Date date = toDate();

        // ISO week day (Mon=1 to Sun=7)
        final int dayOfWeek = 1 + (date.getDay() + MAX_DAY_OF_WEEK) % DAYS_IN_WEEK;
        final Date nearestThu = addDays(date, ISO_THURSDAY - dayOfWeek);
        final int year = nearestThu.getYear();
        final Date jan1 = new Date(year, 0, 1);
        return 1 + dayDiff(nearestThu, jan1) / DAYS_IN_WEEK;
    }

    public static Date addDays(final Date sourceDate, final long days) {
        return new Date(sourceDate.getTime() + (days * MILLIS_DAY));
    }

    public static int dayDiff(final Date firstDate, final Date secondDate) {
        return (int) ((firstDate.getTime() - secondDate.getTime()) / MILLIS_DAY);
    }


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.




--
Stefano Ciccarelli
ste...@indacosoftware.it

Indaco srl
via Sabin, 22 - 40017 San Giovanni in Persiceto (BO) - Italy
tel. +39.051.827762 - fax +39.051.6874570
in...@indacosoftware.it
www.indacosoftware.it

- This e-mail is confidential and may be read, copied and used only by the intended recipient. If you have received it in error, please contact the sender immediately by return e-mail. Please then delete the e-mail and do not disclose its contents to any other person.

- Le informazioni contenute in questa comunicazione sono riservate e destinate esclusivamente alla/e persona/e o all'ente sopra indicati. E' vietato ai soggetti diversi dai destinatari qualsiasi uso, copia, diffusione di quanto in esso contenuto sia ai sensi dell'art. 616 c.p., sia ai sensi del DL n. 196/03. Se questa comunicazione Vi e' pervenuta per errore, Vi preghiamo di rispondere a questa e-mail e successivamente cancellarla dal Vostro sistema.


Reply all
Reply to author
Forward
0 new messages