• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3424
  • Last Modified:

Java Calendar date interval

Can someone please provide me code to help me achieve the following:

Given any date (example: 02-12-2009), I'd like to return String array where the first value of the array is the first date in the associated week (02-08-2009) and the second value is the last date in the associated week  (02-14-2009). This would be based on a week that starts on Sunday and ends on Saturday every time. Also, I need this to work during leap years and non-leap years. Thanks for your help.  
0
jrwalker2
Asked:
jrwalker2
  • 3
  • 2
1 Solution
 
objectsCommented:
see the following for getting the first day of the week

http://helpdesk.objects.com.au/java/how-do-i-get-the-date-of-first-day-of-the-current-week

you can then just loop adding a day each time and using a DateFormat to format the date.

http://helpdesk.objects.com.au/java/how-do-i-format-a-date-as-a-string

Let me know if you need a hand putting it all together

0
 
jrwalker2Author Commented:
I attached my code so far. I have not gotten it to work yet. I have a few problems. First, when I create the initial Gregorian calendar, it does not appear to be following the 0-based month as stated in the javadoc. Second, the first day and the last day in the associated week are not correct. Please help. Thanks  

Actual Output
------------------
Current date: 1/19/2009
First day of week: 1/26/2009
Last day of week: 2/5/2009

Expected Output
-------------------
Current date: 2/19/2009  <-------------I thought that GregorianCaledar month  is 0-based (i.e., Feb=1)
First day of week: 2/15/2009 <----------This is the Sunday of that week
Last day of week: 2/21/2009 <------------This is the Saturday of that week

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.SimpleTimeZone;
import java.util.TimeZone;
 
public class DateUtil 
{
 
	public static void main(String [] args)
	{
		 //Input: February 19, 2009
		GregorianCalendar calendar = new GregorianCalendar(2009,1,19);
		 
		 //Get the interval
		DateUtil dateUtil = new DateUtil();
		String [] interval = dateUtil.getWeekInterval(calendar);
 
		System.out.println("First day of week: " + interval[0]);
		System.out.println("Last day of week: " + interval[1]);
	}
	
	private String[] getWeekInterval(GregorianCalendar cal)
	{
		int day;
		int month;
		int year;
		String firstDate;
		String lastDate;
		
		
		//Current date 
		day = cal.get(Calendar.DAY_OF_MONTH);
		month = cal.get(Calendar.MONTH);
		year = cal.get(Calendar.YEAR);
		System.out.println("Current date: " + month +"/"+day+"/"+year);
		
		//First Day of the week 
		Calendar firstDateofTheWeek = getFirstDayOfThisWeek(cal);
		day = firstDateofTheWeek.get(Calendar.DAY_OF_MONTH);
		month = firstDateofTheWeek.get(Calendar.MONTH);
		year = firstDateofTheWeek.get(Calendar.YEAR);
		firstDate = month +"/"+day+"/"+year;
		
		//Last Day of the week 
		Calendar lastDateofTheWeek = getLastDayOfThisWeek(cal);
		day = lastDateofTheWeek.get(Calendar.DAY_OF_MONTH);
		month = lastDateofTheWeek.get(Calendar.MONTH);
		year = lastDateofTheWeek.get(Calendar.YEAR);
		lastDate = month +"/"+day+"/"+year;
		
		String [] interval = {firstDate,lastDate};
		
		
		return interval;
	}
	
	private Calendar getFirstDayOfThisWeek(GregorianCalendar cal) 
	{
		cal.add(Calendar.DAY_OF_YEAR, Calendar.SATURDAY);
 
		return cal;
	}
	
	private Calendar getLastDayOfThisWeek(GregorianCalendar cal) 
	{
		cal.add(Calendar.DAY_OF_YEAR, Calendar.SATURDAY);
 
		return cal;
	}
	
}

Open in new window

0
 
objectsCommented:
       private DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        private String[] getWeekInterval(Calendar cal)
        {
           // find start of week

           Calendar start = Caledar.getInstance();
           start.setTime(cal.getTime());
           start.set(Calendar.DAY_OF_WEEK, cal.getActualMinimum(Calendar.DAY_OF_WEEK));

           // find end of week

           Calendar end = Caledar.getInstance();
           end.setTime(cal.getTime());
           end.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));

           // format start and end date and return

           return new String[] { df.format(start), df.format(end) };
      }

0
 
jrwalker2Author Commented:
This is great!!! The only thing that I had to change

 return new String[] { df.format(start), df.format(end) };

to

return new String[] { df.format(start.getTime()), df.format(end.getTime()) };

I had to do this in order to use the formatter. Thank you! Your help is very much appreciated.
0
 
objectsCommented:
I made that mistake on purpose to make sure you understood how it worked ;)

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now