Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9206379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:09:46+00:00 2026-06-18T00:09:46+00:00

Following is the code that I am using to calculate the week start date

  • 0

Following is the code that I am using to calculate the week start date and end date for the given month.
Assume week start day is MONDAY and week end day is SUNDAY.
For example, JANUARY, 2013 will have 5 weeks.
If the month starts with Sunday – ignore that day

January 2013
first week – 31-Dec-2012 to 06-Jan-2013
second week – 07-jan-2013 to 13-jan-2013
Third week – 14-jan-2013 to 20-jan-2013
fourth week – 21-jan-2013 to 27-jan-2013
fifth week – 28-jan-2013 to 03-Feb-2013

public static void main(String[] args) {
    List<List<String>> weekdates = getNumberOfWeeks(2013, Calendar.JULY);
    for(List<String> weekDatesLoop:weekdates){
        System.out.println("Start day: "+weekDatesLoop.get(0).toString());
        System.out.println("End day: "+weekDatesLoop.get(1).toString());
    }
  }
public static List<List<String>> getNumberOfWeeks(int year, int month) {
        System.out.println("Month Id: "+month);
        month = month-1;
        System.out.println("Month Id: " + month);
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        List<List<String>> weekdates = new ArrayList<List<String>>();

        List<String> dates = new ArrayList<String>();
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        c.set(Calendar.DAY_OF_MONTH, 1);
        dates.add(format.format(c.getTime()));
        //int numOfWeeksInMonth = 1;
        while (c.get(Calendar.MONTH) == month) {
          //System.out.println(c.get(Calendar.DAY_OF_WEEK) );

          if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            dates.add(format.format(c.getTime()));
            weekdates.add(dates);
          }
          else if (c.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) {
            dates = new ArrayList<String>();
            dates.add(format.format(c.getTime()));
            //numOfWeeksInMonth++;
          }
          c.add(Calendar.DAY_OF_MONTH, 1);
        }
        if(dates.size() < 2){
          c.add(Calendar.DAY_OF_MONTH, -1);
          dates.add(format.format(c.getTime()));
          weekdates.add(dates);
        }
        System.out.println(weekdates);
        return weekdates;
      }

I am still working on this.
Can anyone please help me in fixing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:09:47+00:00Added an answer on June 18, 2026 at 12:09 am

    I get the answer with the following code

    List<List<String>> getNumberOfWeeks(int year, int month) {
            SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
            List<List<String>> weekdates = new ArrayList<List<String>>();
            List<String> dates;
            Calendar c = Calendar.getInstance();
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month);
            c.set(Calendar.DAY_OF_MONTH, 1);
            while (c.get(Calendar.MONTH) == month) {
                    dates = new ArrayList<String>();
                  while (c.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
                    c.add(Calendar.DAY_OF_MONTH, -1);
                  }
                  dates.add(format.format(c.getTime()));
                  c.add(Calendar.DAY_OF_MONTH, 6);
                  dates.add(format.format(c.getTime()));
                  weekdates.add(dates);
                  c.add(Calendar.DAY_OF_MONTH, 1);
            }
            System.out.println(weekdates);
            return weekdates;
          }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that works without using throw however when I use
I came across the following code that compiles fine (using Visual Studio 2005): SomeObject
Visual Studio 2008 I am using the following source code that compiles ok using
I am using Lazarus v0.9.30 (32 bit compiler). I have the following code that
In the following assembly code that I dumped out using objdump : lea 0x0(%esi,%eiz,1),%esi
Hey everyone. The following is the code that I am using to play random
I have the following block of jQuery code that I'm using to copy some
I am using the following code to save values from a settings view that
I'm using the following code to update a record. Bad thing is that it's
I implemented the following code that use the data points in dat to calculate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.