Trying to find Week-of-year for a given date.
Using the below piece of code. [ Java 1.7 ]
Problem:
29-Dec-2012 = week 52
30-Dec-2012 = week 1
30-Dec-2013 = week 52
31-Dec-2013 = week 1
When day-of-year = 365 or 366 , it gives week-of-year to 1.
Looks like some modulo 52 happens.
How to fix this ?
import java.util.Calendar;
import java.util.GregorianCalendar;
public class cal2{
public static void main (String[] args) {
Calendar mycal = GregorianCalendar.getInstance();
mycal.setLenient(false);
int year = 2012;
int month = 11; //0=Jan, 11=Dec
int date = 29;
mycal.set(year , 0 , 1);
mycal.setFirstDayOfWeek(mycal.get(mycal.DAY_OF_WEEK));
mycal.set(year,month,date);
System.out.println("\n>>>>>>>>WEEK :"+mycal.get(mycal.WEEK_OF_YEAR));
// System.out.println("\nDATE :"+mycal);
}
}