3

How to get week of year in gwt ? I have tried to get the week of year but wasn't able to. Please guide me. I have tried using the below code but it prints week of the day not of the entire year.

DateTimeFormat format = DateTimeFormat.getFormat("c"); 
String dayOfWeek = format.format(new Date());
E12CommonUtils.printOnConsole("dayOfWeek ="+dayOfWeek);
Alkis Kalogeris
  • 17,044
  • 15
  • 59
  • 113
  • There's only getStartingDayOfWeek() in CalendarUtil class . From that how can we get week of the entire year ? – Savita Yadav Jul 22 '15 at 10:18
  • possible duplicate of [How to get week of year figure in GWT](http://stackoverflow.com/questions/23938938/how-to-get-week-of-year-figure-in-gwt) – Alkis Kalogeris Jul 22 '15 at 10:23
  • Nah i tried that way but it gives output as 0 . – Savita Yadav Jul 22 '15 at 10:43
  • Really? I can understand if there is an error in the calculation, but 0? Can you include the code exactly as you tried it? – Alkis Kalogeris Jul 22 '15 at 11:00
  • Sure u can check here `Date date = new Date(); Date yearStart = new Date(date.getYear(), 0, 0); int week = (int) (date.getTime() - yearStart.getTime())/(7 * 24 * 60 * 60 * 1000); E12CommonUtils.printOnConsole("week is ============="+week);` – Savita Yadav Jul 22 '15 at 11:13
  • 1
    There are some parentheses that are missing. Try this `int week = (int) ((date.getTime() - yearStart.getTime())/(7 * 24 * 60 * 60 * 1000));` – Alkis Kalogeris Jul 22 '15 at 11:17
  • `Date date = new Date(); Date yearStart = new Date(date.getYear(),0,0); int week = (int) ((date.getTime() - yearStart.getTime())/(7 * 24 * 60 * 60 * 1000)); System.out.println("yearStart is ======="+yearStart); System.out.println("Week is ======="+week);` I tried this way but output i am confused .. – Savita Yadav Jul 22 '15 at 11:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83965/discussion-between-alkis-and-savita-yadav). – Alkis Kalogeris Jul 22 '15 at 11:22
  • Please don't forget to accept the answer, so this thread can be considered closed. – Alkis Kalogeris Jul 22 '15 at 11:44

1 Answers1

0

The below will do what you requested.

Date date = new Date(); 
Date yearStart = new Date(date.getYear(), 0, 0); 

double week = (date.getTime() - yearStart.getTime())/(double)(7 * 24 * 60 * 60 * 1000);
System.out.println((int)Math.ceil(week));
Alkis Kalogeris
  • 17,044
  • 15
  • 59
  • 113
  • 1
    Are you sure? 2015-01-06 is in 2015-W02, not 2015-W01; and 2016-01-01 is in 2015-W53. This is why [`java.text.SimpleDateFormat`](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) has a separate symbol for "year" and "week year". A while back, I worked on an implementation in C: https://github.com/GNOME/libxslt/blob/d20a5ab03c195870e3d27b14965d1ed6f3ae0aed/libexslt/date.c#L2121-L2188 It's definitely not that easy. – Thomas Broyer Jul 22 '15 at 13:32
  • Yes it's not that easy. I've seen the Calendar implementation in Java and it does a lot more. It event depends on which day the week starts. Moreover the first week of the year might be in the previous year. I've took the implementation from here http://stackoverflow.com/questions/23938938/how-to-get-week-of-year-figure-in-gwt and I adjusted it per the specification of the OP. I posted this as an answer since the OP agreed that it solved the problem at hand. – Alkis Kalogeris Jul 22 '15 at 13:39
  • I had one more doubt how to count previous week year ? I mean we are getting week year for 2015 but on previous button if user clicks then it should get week years for 2013,2014 and on next button then it should get week year for 2015,2016,2017 then how they are going to get week years on click of next or previous buttons? – Savita Yadav Jul 23 '15 at 08:32
  • If you want it so generic, it would be better to have it in the server and just request the number from an RPC. You could do `Date yearStart = new Date(USER_SELECTION, 0, 0);` but this won't be exactly correct. The starting week of the year is not the starting of the year. The first week of 2014 starts on `Thu Dec 26 2013`. The code above calculates weeks from the starting of the year explicitly, but the correct way to do it is to calculate the first week in terms of the calendar. ` – Alkis Kalogeris Jul 23 '15 at 08:39
  • Ok I'm in the previous chat. – Alkis Kalogeris Jul 23 '15 at 08:49