Results 1 to 10 of 10
  1. #1
    jwdrumhaven is offline Member
    Join Date
    Sep 2014
    Posts
    4
    Rep Power
    0

    Default Date compasrisons not working

    Hi,
    I am having a hard time getting some caparisons with Date to function correctly. Im sure its probably something small, but im just not seeing it.

    I have a driver program and two classes, ZodiacDriver , ZodiacSign and ZodiacTable. In the driver program I build an arraylist of zodiacsigns in zodiactable. That all appears to be working I then pass a birthdate and the zodiac table to a method, determineSign. This is where I am having trouble. Right now Im just trying to match the incoming birthdate with a zodiac's date in the table. however no matter what date I enter, i get pisces, which happens to be at the bottom of the list. If I remove pisces from the list I get the current last zodiac.

    Here is my code:

    ZodiacDriver
    Java Code:
    package zodiac;
    
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.*;
    /** Driver program for Zodiac
     * 
     * @author joew
     *
     */
    public class ZodiacDriver {
    	
           
        
    	public static void main(String[] args) throws ParseException {
    		// TODO Auto-generated method stub
                Date birthdate = getBirthdate();
    			ZodiacTable zodiac = createZodiacTable();
    			
    			try
    			{
    			System.out.println(determineSign(birthdate,zodiac));
    			}
    			catch (NullPointerException npRef)
    			{
    				System.out.println("Line 18:" + npRef.toString());
    			}
    	}
    	private static String determineSign(Date birthdate, ZodiacTable zodiac) throws ParseException {
    		// TODO Auto-generated method stub
    			String returnString = null;                   
    			Date date = birthdate;                                           //Get birthdate and convert
                                                                                                      // to a date and format in variable compDate
    			//System.out.println(zodiac.g);
    			Calendar compDate = Calendar.getInstance();
    			DateFormat format = new SimpleDateFormat("MM/dd/");
    			format.format(date);
    			compDate = format.getCalendar();
    			
    			compDate.clear(Calendar.HOUR);                                     //clear out the fields I dont want in compDate
    			compDate.clear(Calendar.HOUR_OF_DAY);
    			compDate.clear(Calendar.MINUTE);
    			compDate.clear(Calendar.SECOND);
    			compDate.clear(Calendar.MILLISECOND);
    			compDate.clear(Calendar.YEAR);
    			
    				
    		//test:
    		for (ZodiacSign z : zodiac.getZodiac())                              //Loop through the zodiacsigns in the zodiactable
                  
    			
    		{
    			
    			Calendar startDate = Calendar.getInstance();               //get the start date from the zodiac sign and format
    			format.format(z.getStartDate());                               //the same way as compDate
    			startDate = format.getCalendar();
    			
    			startDate.clear(Calendar.HOUR);                               //Again, clear out the fields not wanted
    			startDate.clear(Calendar.HOUR_OF_DAY);
    			startDate.clear(Calendar.MINUTE);
    			startDate.clear(Calendar.SECOND);
    			startDate.clear(Calendar.MILLISECOND);
    			startDate.clear(Calendar.YEAR);
    			
    			Calendar endDate = Calendar.getInstance();          //Do the same for the endDate, by formatting and clearing
    			format.format(z.getEndDate());
    			endDate = format.getCalendar();
    			
    			endDate.clear(Calendar.HOUR);
    			endDate.clear(Calendar.HOUR_OF_DAY);
    			endDate.clear(Calendar.MINUTE);
    			endDate.clear(Calendar.SECOND);
    			endDate.clear(Calendar.MILLISECOND);
    			endDate.clear(Calendar.YEAR);
    							
    			
    		//	if (compDate.before(startDate) && compDate.before(endDate))
    							
    		  //      {
    			//		continue test;
    			//	}
    			
    						
    			 if (compDate.equals(startDate) )		//Now the dates should be in 							
    			{                                                      //the same format. See if they equal each other 
    				
    			 
    				returnString = "Your zodiac sign is " + z.getName() + " " + z.getStartDate() + " "
    				+ z.getEndDate();
    				 
    				 
    				
                   }			
    		}
    			
    		return returnString;               //return the zodiac found in the comparison
    		
    	}
    
    	public static ZodiacTable createZodiacTable() throws ParseException {
    		 
    		 SimpleDateFormat f = new SimpleDateFormat("MM/dd/yyyy"); 
    		
    		 ZodiacSign aries = new ZodiacSign("Aries",f.parse("03/21/2015"),f.parse("04/20/2015"));
    		 ZodiacSign taurus = new ZodiacSign("Taurus", f.parse("04/21/2015"), f.parse("05/20/2015"));
    		 ZodiacSign gemini = new ZodiacSign("Gemini", f.parse("05/21/2015"),f.parse("06/21/2015"));
    		 ZodiacSign cancer = new ZodiacSign("Cancer",f.parse("06/22/2015"),f.parse("07/22/2015"));
    		 ZodiacSign leo = new ZodiacSign("Leo",f.parse("07/23/2015"),f.parse("08/23/2015"));
    		 ZodiacSign virgo = new ZodiacSign("Virgo",f.parse("08/24/2014"),f.parse("09/23/2014"));
    		 ZodiacSign libra = new ZodiacSign("Libra", f.parse("09/24/2014"), f.parse("10/23/2014"));
    		 ZodiacSign scorpio = new ZodiacSign("Scorpio", f.parse("10/24/2014"), f.parse("11/22/2014"));
             ZodiacSign saggit = new ZodiacSign("Sagittarius", f.parse("11/23/2014"), f.parse("12/21/2104"));
             ZodiacSign capri = new ZodiacSign("Capricorn", f.parse("12/22/2014"), f.parse("01/20/2015"));
             ZodiacSign aquar = new ZodiacSign("Aquarius", f.parse("01/21/2015"), f.parse("02/19/2015"));
             ZodiacSign pisces = new ZodiacSign("Pisces", f.parse("02/20/2015"), f.parse("03/20/2015"));
             
             ZodiacTable table = new ZodiacTable();
                      
             
              table.addZodiac(aries);
              table.addZodiac(taurus);
    		  table.addZodiac(gemini);
    		  table.addZodiac(cancer);
    		  table.addZodiac(leo);
    		  table.addZodiac(virgo);
    		  table.addZodiac(libra);
    		  table.addZodiac(scorpio);
    		  table.addZodiac(saggit);
    		  table.addZodiac(capri);
    		  table.addZodiac(aquar);
    		  table.addZodiac(pisces);
    
    			 System.out.println(table);
    		 return table;
    		 
    		}
    	
    	public static  Date getBirthdate() throws ParseException {
    		Date bdate = null;
    		Scanner keyboard = new Scanner(System.in);					
    		System.out.println("Please enter your birthdate.(Format 03/28/1968)");
    		String birthDate = keyboard.next();
    		
    		SimpleDateFormat f = new SimpleDateFormat("MM/dd/yyyy");
    		f.setLenient(false);
    		
    		try 
    		{
    			bdate = f.parse(birthDate);
    			keyboard.close();
    			//return date;
    		}
    		
    		catch (ParseException peRef)
    		{
    			System.out.println("Line 99: Exception"
    					+ peRef.toString() + " " + "Invalid Date. Goodbye");
    			System.exit(0);
    		}
    		
    		System.out.println(bdate);
    		return bdate;
    		
    	}
    		
    
    }
    ZodiacSign
    Java Code:
    package zodiac;
    import java.util.*;
    /** Represents a zodiac sign
     * @author joew
     * @version 1.0
     */
    public class ZodiacSign {
    	private  String name;
    	private  Date startDate;
    	private  Date endDate;
    	
    	/**
    	 * @param name
    	 * @param startDate
    	 * @param endDate
    	 */
    	public ZodiacSign(String name, Date startDate, Date endDate) {
    	
    		//super();
    		this.name = name;
    		this.startDate = startDate;
    		this.endDate = endDate;
    			
    	}
    	/**
    	 * @return the name
    	 */
    	public String getName() {
    		return name;
    	}
    	/**
    	 * @param name the name to set
    	 */
    	public void setName(String name) {
    		this.name = name;
    	}
    	/**
    	 * @return the startDate
    	 */
    	public Date getStartDate() {
    		return this.startDate;
    	}
    	/**
    	 * @param startDate the startDate to set
    	 */
    	public void setStartDate(Date startDate) {
    		this.startDate = startDate;
    	}
    	/**
    	 * @return the endDate
    	 */
    	public Date getEndDate() {
    		return endDate;
    	}
    	/**
    	 * @param endDate the endDate to set
    	 */
    	public void setEndDate(Date endDate) {
    		this.endDate = endDate;
    	}
    //	@Override
    	//public String toString() {
    		//return name;
    	//}
    
    	
    }
    ZodiacTable

    Java Code:
    package zodiac;
    
    import java.util.ArrayList;
    
    /**
     * Represents a zodiac table
     * @author joew
     *
     */
    public class ZodiacTable {
    	
    	private ArrayList<ZodiacSign> zodiac;
    		
    	
        public ZodiacTable() {
    		
        	zodiac = new ArrayList <ZodiacSign>();
    		
    	}
    
    
    	/**
    	 * @return the zodiac
    	 */
    	public ArrayList<ZodiacSign> getZodiac() {
    		
    		return zodiac;
    	}
    
    
    	/**
    	 * @param zodiac the zodiac to set
    	 */
    	public void addZodiac(ZodiacSign zodiac) {
    	
    		this.zodiac.add(zodiac);
    		
    	}
    
    
    
    
    	
    }

    Any assistance would be greatly appreciated.. thanks!
    Last edited by jwdrumhaven; 09-24-2014 at 05:12 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Date compasrisons not working

    How are you trying to debug the code? I use println() statements. Add a println() statement just before the statement that compares the dates that prints out the two dates and the value returned by the methods used to compare the dates. The print out will show you what the computer sees when the code is executed which should help you see the problem.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jwdrumhaven is offline Member
    Join Date
    Sep 2014
    Posts
    4
    Rep Power
    0

    Default Re: Date compasrisons not working

    I tried that and I think its what I would expect to see.. I will try again.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Date compasrisons not working

    It's hard to say what you printed out, because all the debugging println() statements have been removed from the posted code.

    The code has a lot of unexplained statements in the determineSign() method. Can you add some comments explaining what that is all about? Testing if a date is between two other dates should only take a couple of statements.
    Last edited by Norm; 09-24-2014 at 04:54 AM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jwdrumhaven is offline Member
    Join Date
    Sep 2014
    Posts
    4
    Rep Power
    0

    Default Re: Date compasrisons not working

    I just edited the origianl post to include comments of what I think should be happening. If I am wrong please let me know. Thanks

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Date compasrisons not working

    I just edited the origianl post to include comments
    I don't see any of them in the determineSign() method. That is were the logic needs to be explained.

    The comments in the ZodiacSIgn class are almost redundant. The methods are so simple and well named.

    The test for a value in a range should only require two tests: greater than the lower bound and less than the upper bound. What is all the code in determineSign() for?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    jwdrumhaven is offline Member
    Join Date
    Sep 2014
    Posts
    4
    Rep Power
    0

    Default Re: Date compasrisons not working

    Sorry.. it looks like it did not save my edit. In determineSign, compDate, startDate (from z.getStartDate) and endDate(from z.getEndDate) all get formatted with SimpleDateFormat, and I clear out the fields that I dont want to use in all three. Then I compare compDate with startDate and return a string containing the zodiac name , zodiac startdate and zodiac end date..

    I hope this helps.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Date compasrisons not working

    SimpleDateFormat.format() does not format a Date.
    It returns a String representation of the Date in the format specified.

    Date objects do not have a format...they are simply a long, representing milliseconds since 1970.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Date compasrisons not working

    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Date compasrisons not working

    I still don't see any comments in the code.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 5
    Last Post: 02-16-2012, 10:26 AM
  2. Replies: 4
    Last Post: 07-27-2011, 12:57 PM
  3. Replies: 2
    Last Post: 05-12-2011, 05:52 PM
  4. date is not working properly
    By newnewgen in forum New To Java
    Replies: 1
    Last Post: 10-12-2010, 10:04 AM
  5. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 09:32 PM

Posting Permissions

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