Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Calendar Issue

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,621
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Calendar Issue

    Ok, I have a problem where: given two dates and a frequency, a list of dates are returned in that range based on that frequency.

    The dates are formatted mm/dd/yyyy where leading zeros are not included.
    The frequency is formatted in one of 3 ways:
    1) Daily - Occurs every day of week
    2) X... - Occurs every day of week except the dates after the X
    3) ... - Occurs only on the days indicated
    The days in the frequency are determined by a number, ranging 1 to 7 where 1 is Monday and 7 is Sunday. So, the frequency X6 would say: "Every day except Saturday" and the frequency 126 would say: "Only on Monday, Tuesday, and Saturday".

    Now, to accomplish this task, I have a method that returns a List<Date>. Inside that method, I have a boolean array of 7 where each index is a day of the week (where Monday is index 0). I then set the booleans true/false based on the frequencies. After that, I compute each day in the range and check its Day Of Week to see if it is in the frequency. If it is, I add it to my List<Date>, if not, I exclude it. Here is my code for that method:
        public List<Date> getDates(String star,String end,String frequency)
        {
        	//Where frequencies[0] == Monday
        	boolean[] frequencies = new boolean[7];
     
        	if(frequency.equalsIgnoreCase("Daily"))
        	{
        		for(int i=0;i<frequencies.length;i++)
        		{
        			frequencies[i]=true;
        		}
        	}
        	else if(frequency.contains("X") || frequency.contains("x"))
        	{
        		for(int i=0;i<frequencies.length;i++)
        		{
        			frequencies[i] = true;
        		}
        		String n = frequency.substring(1);
        		for(int i=0;i<n.length();i++)
        		{
        			int v = Integer.parseInt(n.substring(i,i+1));
        			frequencies[v-1] = false;
        		}
        	}
        	else
        	{
        		for(int i=0;i<frequency.length();i++)
        		{
        			int v = Integer.parseInt(frequency.substring(i,i+1));
        			frequencies[v-1] = true;
        		}
        	}
     
     
        	String[] eSplit = star.split("/");
        	String[] dSplit = end.split("/");
        	Date d1 = new Date(Integer.parseInt(eSplit[2]),Integer.parseInt(eSplit[0])-1,Integer.parseInt(eSplit[1]));
        	Date d2 = new Date(Integer.parseInt(dSplit[2]),Integer.parseInt(dSplit[0])-1,Integer.parseInt(dSplit[1]));
        	List<Date> dates = new ArrayList<Date>();
    		long interval = 1000 * 60 * 60 * 24;
    		long endTime = d2.getTime();
    		long curTime = d1.getTime();
    		while (curTime <= endTime)
    		{
    			Date temp = new Date(curTime);
    			Calendar check = new GregorianCalendar(temp.getYear(), temp.getMonth(), temp.getDate());
    			check.setFirstDayOfWeek(Calendar.MONDAY);
    			int dayOfWeek = check.get(Calendar.DAY_OF_WEEK);
    			if(frequencies[dayOfWeek-1])
    		  		dates.add(new Date(curTime));
    		  	curTime += interval;
    		}
    		return dates;
        }

    The problem I am having is that the dates it returns are incorrect. Specifically, it would seem it is 1 day early and for some it doesn't even find dates. For example, for in restriction of:
    1/4/2011 to 2/16/2011 for 6:
    it returns:
    1/7/2011
    1/14/2011
    1/21/2011
    1/28/2011
    2/4/2011
    2/11/2011
    while it should return:
    1/8/2011
    1/15/2011
    1/22/2011
    1/29/2011
    2/5/2011
    2/12/2011

    Another example is that for the restriction of:
    12/21/2010 to 1/3/2011 for 6:
    it returns:
    12/24/2010
    12/31/2010
    while it should return:
    12/25/2010
    1/1/2011


    Any clues as to what is happening? I need to figure this out fairly quickly.
    Last edited by aussiemcgr; October 11th, 2010 at 12:25 PM. Reason: Fixed portion of code, but still having issues
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,621
    Thanks
    25
    Thanked 322 Times in 300 Posts

    Default Re: Calendar Issue

    I figured it out. Apparently Calendar.setFirstDayOfWeek(Calendar.MONDAY); doesnt exactly set the first day of week to be Monday. It is more complicated than that. Either way, I figured it out.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. I'm having trouble with date and calendar class
    By kiph in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 02:56 AM
  2. calendar javascript required
    By sagar13 in forum Paid Java Projects
    Replies: 0
    Last Post: February 4th, 2010, 11:57 PM
  3. Calendar help
    By moonieass13 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 31st, 2009, 12:43 AM
  4. calendar
    By subhvi in forum AWT / Java Swing
    Replies: 2
    Last Post: September 29th, 2009, 11:02 PM
  5. which calendar to use?
    By rptech in forum AWT / Java Swing
    Replies: 6
    Last Post: August 30th, 2009, 03:18 PM