CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2009
    Posts
    38

    [RESOLVED] Calendar week view

    I want to create a week view but I can't figure out how to get the first date of this week (03/01/2010) and the last date (09/01/2010). I found this script but I get as first date 10/01/2010 and as last date 09/01/2010 and I don't see why I get 10/01/2010 as first date.

    Code:
    // Code to test it.
    GregorianCalendar calendar = new GregorianCalendar();
    		 
    		 //Get the interval
    		String [] interval = calendarModel.getWeekInterval(calendar);
     
    		System.out.println("First day of week: " + interval[0]);
    		System.out.println("Last day of week: " + interval[1]);
    
    private DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
         public String[] getWeekInterval(Calendar cal)
         {
            // find start of week
    
            Calendar start = Calendar.getInstance();
            start.setTime(cal.getTime());
            start.set(Calendar.DAY_OF_WEEK, cal.getActualMinimum(Calendar.DAY_OF_WEEK));
    
            // find end of week
    
            Calendar end = Calendar.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.getTime()), df.format(end.getTime()) };
       }
    	
    	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;
    	}

  2. #2
    Join Date
    Apr 2007
    Posts
    425

    Re: Calendar week view

    Pay attention to what you are reading and displaying. Using today's date arbitrarily you get:

    01/03/2010 and 01/09/2010

    as the boundaries.

    I should mention now that

    Code:
    	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;
    	}
    are both the same method, they do the same thing, and you should realize that you are adding to the current date object a year modifier, with the constant Calendar.SATURDAY which is equal to 7.
    ------
    If you are satisfied with the responses, add to the user's rep!

  3. #3
    Join Date
    Apr 2007
    Posts
    425

    Re: Calendar week view

    Didn't think to post this the first time around, but if you want a full understanding of date and time as it relates to Java and the various wrapper classes it provides, this is a very informative and a good read for you, or anyone for that matter to go over:

    http://www.odi.ch/prog/design/datetime.php
    ------
    If you are satisfied with the responses, add to the user's rep!

  4. #4
    Join Date
    Aug 2009
    Posts
    38

    Re: Calendar week view

    Thank you, that link helped me a lot. I fixed it now

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured