Is there any method which will return the current week number of the year?

Printable View

- 05-31-2007, 03:33 PMleventCurrent week number method
Is there any method which will return the current week number of the year?

- 05-31-2007, 03:38 PMValeriano
Check javadoc of GregorianCalendar class.

You can use following method:

Code:`System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR));`

- 02-01-2010, 05:33 AMsandip.wagholeHere is the complte solution
/ ================================================== ==

Method: Get the desired Date format for the date

Developed By: Sandip Waghole [29-Jan-2010]

================================================== == /

public String getWeekNo(String strDate)

{

// input Date Format : M/dd/yyyy

int weekNo=0,i=0;

String strWeekNo=null;

int noOfDaysInTheYear=365;

int WEEK_STARTS_ON = 1; // Define the day on which week starts Sunday/Monday 1:Sunday 2:Monday

int firstDayNoInFirstWeekOfPresentYear=0; // Inititalize teh day on which week is starting in present year

int firstDayOfPresentYear=0; // Inititlize the 1st day of the present year whether Sunday/Monday/.....

int[] monthDaysArray = {31,28,31,30,31,30,31,31,30,31,30,31}; // Define array of the days as per months

int todaysDayNoInPresentYear=0;

int daysLateByFirstWeekStartedAfterYearStarted=0;

int intTemp=0;

//strDate="08/24/2000"; // For test purpose

StringTokenizer strDateTok = new StringTokenizer(strDate, "/ ");

int month = Integer.parseInt(strDateTok.nextToken());

int day= Integer.parseInt(strDateTok.nextToken());

int year = Integer.parseInt(strDateTok.nextToken());

GregorianCalendar cal = new GregorianCalendar();

// Check if present year is leap year

boolean boolIsLeapYear = cal.isLeapYear(year);

// If it is boolean year then add 1 to total days in the year & add one more day to february

if(boolIsLeapYear)

{

noOfDaysInTheYear=noOfDaysInTheYear+1;

monthDaysArray[1]=monthDaysArray[1]1;

}

// Find the 1st day of this year

Calendar calObj = new GregorianCalendar(year, Calendar.JANUARY, 1);

firstDayOfPresentYear = calObj.get(Calendar.DAY_OF_WEEK);

int intRemoveNoOfDaysFromWeek=0;

// # Find the day no of prsent day

for(i=0;i<month;i+) // get no of days till present year

{

intTemp = intTemp monthDaysArray[i];

}

todaysDayNoInPresentYear = intTemp - (monthDaysArray[month-1]-day);

if(firstDayOfPresentYear==6 || firstDayOfPresentYear==7) // If first Day is Friday or Saturday then it is week

{

// Identify the the day no on which 1st week of present year is starting

firstDayNoInFirstWeekOfPresentYear = 7 - firstDayOfPresentYear WEEK_STARTS_ON 1;

// Find delay in the 1st week start after r=the year start

daysLateByFirstWeekStartedAfterYearStarted = firstDayNoInFirstWeekOfPresentYear - 1;

// Now week is starting from Sunday

weekNo = (Integer)((todaysDayNoInPresentYear-daysLateByFirstWeekStartedAfterYearStarted)/7);

// Find the day no of today

intTemp = (todaysDayNoInPresentYear-daysLateByFirstWeekStartedAfterYearStarted) % 7;

if(intTemp > 0)

{

weekNo=weekNo+1;

}

else

{

weekNo=weekNo;

}

}

else

{

// 1st week is starting on 1st Of January

firstDayNoInFirstWeekOfPresentYear=firstDayOfPrese ntYear;

// Remove no. of days from the 1st week as week is starting from odd Sunday/Monday/Tuesday/Wednesday/Thursday

intRemoveNoOfDaysFromWeek = 7-firstDayOfPresentYear 1; // 1 added as include start day also

// So one week will be added in no. of weeks

weekNo = (Integer)((todaysDayNoInPresentYear-intRemoveNoOfDaysFromWeek)/7);

// Find the day no of today

intTemp = (todaysDayNoInPresentYear-intRemoveNoOfDaysFromWeek) % 7;

weekNo = weekNo +1; // As 1st weeks days are reduced from the todays day no in the year

if(intTemp > 0)

{

weekNo=weekNo+1;

}

else

{

weekNo=weekNo;

}

// Remove the no. of days from the week 1

}

strWeekNo=Integer.toString(weekNo);

return strWeekNo;

}

// Any issues please mail on sandip.waghole@gmail.com