0

if i try to get the actual week of year like this:

Calendar cal = Calendar.getInstance();
actualWeek = cal.get(Calendar.WEEK_OF_YEAR);

i get 35 for actualWeek

but for

cal.set(2013, 7, 30);
int week = cal.get(Calendar.WEEK_OF_YEAR);

i get 40

and 42 for

cal.set(2013, 8, 18);
int week = cal.get(Calendar.WEEK_OF_YEAR);

what is wrong with my calendar? it should be 35 for 8/29 and 8/30 and 38 for 9/18

ntm
  • 731
  • 2
  • 13
  • 37
  • What sort of week-numbering are you expecting? There are *lots* of different types of week-numbering available. – Jon Skeet Aug 29 '13 at 18:15
  • 3
    Could it be due to calendar month starts from ZERO?http://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#MONTH – kosa Aug 29 '13 at 18:15
  • @Nambari thats why i use 2013, 7, 30 for 2013/08/30 – ntm Aug 29 '13 at 18:17
  • @JonSkeet i'm expecting the number of the week of the year starting at 1 like i get it in the first example (35 for the actual week). i don't think that i can get different types when i use the same method. – ntm Aug 29 '13 at 18:19
  • @NathanMaier: Sure, it should give consistent results - but it would really help if you'd explain where you got the numbers you were expecting. "The number of the week of the year" is not one well-defined concept. – Jon Skeet Aug 29 '13 at 18:20
  • I just ran your example and got 35 for both top 2 cases. – kosa Aug 29 '13 at 18:21
  • 1
    I looked at your profile and it seems like you are posting from Germany? Week of the year is local and it is different in USA and Germany. Search issue on SOF and you will find similar posts. – Gokhan Arik Aug 29 '13 at 18:22
  • I don't know solution but using `GregorianCalendar()` might be solution, give it a try – Gokhan Arik Aug 29 '13 at 18:24
  • @JonSkeet i don't know how to define it better. i mean that the week from 2013/1/1 to 1/5 was week 1, the week from 1/6 to 1/12 was week 2 and the week from 12/29 to 12/31 will be week 52. and next year it starts at 1 again. – ntm Aug 29 '13 at 18:25
  • @GokhanArik whats the difference, except that week starts at monday in germany and at sunday in us? monday/sunday makes no difference because 08/29 and 08/30 are thursday and friday – ntm Aug 29 '13 at 18:26
  • I found that but it is about end of the year. I am not sure if it applies to you. As I said earlier try to use Gregorian Calendar. http://stackoverflow.com/a/4608695/1106598 – Gokhan Arik Aug 29 '13 at 18:30
  • @GokhanArik using GregorianCalendar doesn't change anything – ntm Aug 29 '13 at 18:31
  • @GokhanArik that doesn't really apply to my problem because it's only about the last and first week of a year – ntm Aug 29 '13 at 18:34
  • 1
    i have no idea why but after i updated android studio and downloaded the sdk for api 18 it works! – ntm Aug 29 '13 at 18:38

0 Answers0