Results 1 to 3 of 3
  1. #1
    levent Guest

    Default Current week number method

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

  2. #2
    Valeriano is offline Member
    Join Date
    May 2007
    Posts
    10
    Rep Power
    0

    Default

    Check javadoc of GregorianCalendar class.

    You can use following method:

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

  3. #3
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Smile Here 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

Similar Threads

  1. Replies: 0
    Last Post: 03-01-2008, 10:01 PM
  2. Current month
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-07-2008, 08:40 AM
  3. Getting current userís name
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-07-2007, 12:04 PM
  4. how to get the current date and time
    By valery in forum New To Java
    Replies: 1
    Last Post: 08-03-2007, 06:05 PM
  5. How can i get current directory?
    By Ashley in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 01:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •