Link to home
Start Free TrialLog in
Avatar of jrwalker2
jrwalker2

asked on

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.  
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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

Avatar of jrwalker2
jrwalker2

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
I made that mistake on purpose to make sure you understood how it worked ;)