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 4 of 4

Thread: Comparing Two Dates

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Comparing Two Dates

    Ok, so I'm ripping my hair out over this.

    I have two 3 GregorianCalendar objects. Due to reasons unknown, the .after() and .before() methods are not working correctly all the time, so I'm having to make a method my own to get some sort of consistency.

    Basically, I'm just wanting to check if a Date is between (inclusively) a range of dates.

    After trying a bunch of different things (all with complete failure), I tried to make the most concise set of conditional statements I could. But, it still doesn't work. Can anyone find the logic problem in this?

    public boolean withinTimeFrame(GregorianCalendar g)
    	{
    		int year = g.get(GregorianCalendar.YEAR);
    		int month = g.get(GregorianCalendar.MONTH);
    		int day = g.get(GregorianCalendar.DAY_OF_MONTH);
    		int yearS = StartDate.get(GregorianCalendar.YEAR);
    		int monthS = StartDate.get(GregorianCalendar.MONTH);
    		int dayS = StartDate.get(GregorianCalendar.DAY_OF_MONTH);
    		int yearE = EndDate.get(GregorianCalendar.YEAR);
    		int monthE = EndDate.get(GregorianCalendar.MONTH);
    		int dayE = EndDate.get(GregorianCalendar.DAY_OF_MONTH);
     
    		if((year==yearS && month==monthS && day==dayS)||(year==yearE && month==monthE && day==dayE))
    			return true;
    		if(year>yearS)
    		{
    			if(year<yearE)
    				return true;
    			else if(year==yearE)
    			{
    				if(month<monthE)
    					return true;
    				else if(month==monthE)
    				{
    					if(day<=dayE)
    						return true;
    					else
    						return false;
    				}
    				else
    					return false;
    			}
    			else
    				return false;
    		}
    		else if(year==yearS)
    		{
    			if(year<yearE)
    				return true;
    			else if(year==yearE)
    			{
    				if(month>monthS)
    				{
    					if(month<monthE)
    						return true;
    					else if(month==monthE)
    					{
    						if(day<=dayE)
    							return true;
    						else
    							return false;
    					}
    					else
    						return false;
    				}
    				else if(month==monthS)
    				{
    					if(day>dayS)
    					{
    						if(month<monthE)
    							return true;
    						else if(month==monthE)
    						{
    							if(day<=dayE)
    								return true;
    							else
    								return false;
    						}
    						else
    							return false;
    					}
    					else if(day==dayS)
    						return true;
    					else
    						return false;
    				}
    				else
    					return false;
    			}
    			else
    				return false;
    		}
    		else
    			return false;
    	}

    For example, if I have the following 2 inputs:
    Input 1:
    StartDate = September 7, 2011
    EndDate = November 7, 2011
    g = October 6, 2011
    Input 2:
    StartDate = September 7, 2011
    EndDate = November 7, 2011
    g = November 4, 2011


    So, if g is between (inclusively) StartDate and EndDate, the method should return true. Otherwise, it should return false.

    At the moment, I know at least one of these (if not both) is returning true. I'm attempting to isolate the problem.


    EDIT:
    It is returning false for both inputs.
    Last edited by aussiemcgr; June 27th, 2011 at 11:17 AM. Reason: More information
    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
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,106
    Thanks
    65
    Thanked 2,717 Times in 2,667 Posts

    Default Re: Comparing Two Dates

    Could you use this value: time as UTC milliseconds from the epoch
    A long value for the three dates and then just compare them.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Comparing Two Dates

    As Norm suggested, just get the time in milliseconds (Calendar.getTimeInMillis() - the Date class has a similar method) and compare the returned long value. Seems a much easier alternative

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Comparing Two Dates

    This was actually my fault, not the code's. I was checking one direction, but not the other.

    For example, if you had two date ranges:
    Oct 5, 2011 to Dec 10, 2011
    Nov 1, 2011 to Nov 10, 2011

    The first would not conflict with the second, but the second would conflict with the first. I needed the second to conflict with the first AND the first to conflict with the second. I have it working as far as I know.
    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. how to merge java objects with effective dates
    By java4ulok in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 10th, 2011, 01:24 PM
  2. Testing if dates are real or not.
    By cardsfan33 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 22nd, 2011, 07:46 PM
  3. Replies: 9
    Last Post: March 16th, 2011, 11:35 AM
  4. Java/Excel integration, Reading in Dates
    By aussiemcgr in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: July 16th, 2010, 08:38 AM
  5. UTC Dates
    By PedroCosta in forum Java Theory & Questions
    Replies: 4
    Last Post: April 1st, 2010, 11:39 AM