Java Calendar date interval

Posted on 2009-02-20
Last Modified: 2012-05-06
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.  
Question by:jrwalker2
    LVL 92

    Expert Comment

    see the following for getting the first day of the week

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

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

    LVL 1

    Author Comment

    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

    LVL 92

    Accepted Solution

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

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

               // find end of week

               Calendar end = Caledar.getInstance();
               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) };

    LVL 1

    Author Comment

    This is great!!! The only thing that I had to change

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


    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.
    LVL 92

    Expert Comment

    I made that mistake on purpose to make sure you understood how it worked ;)


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    how to extract window origin in spring 2 55
    mergeTwo  challenge 13 53
    powerN  challenge 3 36
    countPairs challenge 7 39
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now