0

I have this code :

Calendar sDateCalendar = Calendar.getInstance(Locale.GERMANY); 
int CurrentWeek = sDateCalendar.get(Calendar.WEEK_OF_YEAR); 
int Year = sDateCalendar.get(Calendar.YEAR);

when the date is ,for example, 29/12/2014

CurrentWeek variable will be 1 because the first week in 2015 is from : December 29, 2014 to : January 4, 2015 BUT the Year variable will have 2014 and not 2015

Can you please help me resolving this issue ?

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
13013Key
  • 9
  • 1
  • 11
  • 2
    Well, it's clear that 29/12/2014 is *not* in 2015. So you must instead want `WEEK_OF_YEAR` to produce "53". Is that right? – Oliver Charlesworth Dec 27 '14 at 14:03
  • If the date belongs to 2014, why would you want the 'year' to be 2015? – Vijay Dev Dec 27 '14 at 14:04
  • When I run this code on my machine `CurrentWeek` is `52`. Are you sure about the result you're describing? – Mureinik Dec 27 '14 at 14:05
  • Take care: there are different definitions as to when a week extending over 31/12+1/1 belongs to the closing year or already to the next. Locale! – laune Dec 27 '14 at 14:06
  • Bob, the fact that (according to Germany's DIN definition) the week already belongs to 2015 as its first week doesn't make 29/12/2014 a day of 2015. And a week split between years is either 5x in one year or 1 in the next. – laune Dec 27 '14 at 14:08
  • 100 % laune but is there any function in java that returns 2015 instead of 2014 – 13013Key Dec 27 '14 at 14:11
  • You'll have to look at the Thursday of the week to which the "current" day (29/12) belongs and take its year. ("Die Kalenderwoche 1 eines Jahres ist diejenige, die den ersten Donnerstag enthält.") You can get the day of the week of the current day and add or subtract to Thursday. – laune Dec 27 '14 at 14:15

3 Answers3

0
static int getYearOfWeek(int year, int month, int day ){
    Calendar cc = new GregorianCalendar( year, month, day );
    int dow = cc.get(Calendar.DAY_OF_WEEK); 
    if( dow == 1 ) dow = 8;   // adjust Sunday
    int diff = Calendar.THURSDAY - dow;
    Calendar thc = new GregorianCalendar( year, month, day + diff );
    return thc.get(Calendar.YEAR);
}
laune
  • 31,114
  • 3
  • 29
  • 42
  • I used this function with 2 examples : first one with 29/12/2014 and the result was 2015 which is 100% correct 2nd example with 28/12/2014 and the result was also 2015 which is wrong – 13013Key Dec 27 '14 at 21:56
  • Ah, drat! Calendar has SUNDAY == 1, which does not agree with German (AT, CH,...) standards. – laune Dec 28 '14 at 06:22
  • So any other suggestions please ? Tx for the help laune – 13013Key Dec 28 '14 at 08:46
0

Well I wrote this method and I guess its the answer of my question :

public static void yearCalc() {
    Calendar sDateCalendar = Calendar.getInstance(Locale.GERMANY);
    int currentWeek = sDateCalendar.get(Calendar.WEEK_OF_YEAR);
    int currentMonth = sDateCalendar.get(Calendar.MONTH);
    int Year = sDateCalendar.get(Calendar.YEAR);
    if (currentWeek == 1 & currentMonth == 11) {
        Year = Year + 1;
    }

    sDateCalendar.set(Calendar.YEAR, Year);
    sDateCalendar.set(Calendar.WEEK_OF_YEAR, currentWeek);

    sDateCalendar.set(Calendar.DAY_OF_WEEK, 2);

    String a = sDateCalendar.getTime().toString();

    System.out.println(a);

    sDateCalendar.set(Calendar.YEAR, Year);
    sDateCalendar.set(Calendar.WEEK_OF_YEAR, currentWeek);

    sDateCalendar.set(Calendar.DAY_OF_WEEK, 8);
    String b = sDateCalendar.getTime().toString();

    System.out.println(b);
}
13013Key
  • 9
  • 1
  • 11
0

As you can see in this post in Java, WEEK_OF_YEAR is local dependent; there are different rules from country to country.

Another issue you may experience: which is the first day of week? (Sunday? or Monday?), so combine your selected locale with sDateCalendar.setFirstDayOfWeek(Calendar.MONDAY); (or SUNDAY).

There is a method to define when a week starts by calling sDateCalendar.setMinimalDaysInFirstWeek(7); so the first week will be the first full (7 days) week of the year.

Community
  • 1
  • 1
Kostas Kryptos
  • 4,081
  • 2
  • 23
  • 24