Results 1 to 6 of 6
  1. #1
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default printing GregorianCalendar default constructor?

    I am having trouble with getting output to be what I want. I can get the output when a user enters their values for month, day and time using a scanner object and they print correctly, but what I want is before they input values, to display the current date. Here is a snippet of what I am working with *these are in two separate files, the first has all the variables etc, the second has the main method.

    What am I doing wrong?


    In one java file:

    Java Code:
    private int numOfGuests, month, day, year;	
    private GregorianCalendar eventDate = new GregorianCalendar();
    
    
    public Event()
    	{ 
    	this("not assigned" , 0, new GregorianCalendar());
    	}
    
    	public int getDay()
    	   {
    		   return day;
    	   }
    	  
    	public void setDay(int currentDay)
    	   {
    		   day = currentDay;
    		   currentDay = eventDate.get(GregorianCalendar.DAY_OF_MONTH);
    	   }
    	   
    	public int getMonth()
    	   {
    		   return month;
    	   }
    	  
    	public void setMonth(int currentMonth)
    	   {
    		   month = currentMonth;
    		   currentMonth = eventDate.get(GregorianCalendar.MONTH);
    	   }
    	   
    	public int getYear()
    	   {
    		   return year;
    		   
    	   }
    	  
    	public void setYear(int currentYear)
    	   {
    		   year = currentYear;
    		   year = eventDate.get(GregorianCalendar.YEAR);
    	   }
    
    	public GregorianCalendar getEventDate() {
    		return eventDate;
    	}
    
    	public void setEventDate(GregorianCalendar eventDate) {
    		this.eventDate = eventDate;
    	}


    in separate java file:


    Java Code:
    Event Values;				
        	
    Values = new Event();
        	
    System.out.println("***********************");
        	
    
    System.out.println
        		("Event Date: " + Values.getMonth() + "-" + Values.getDay() + "-" + Values.getYear());
    This gives me 0-0-0 as a result instead of current date.

    Thanks for the help!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: printing GregorianCalendar default constructor?

    Your setters and getters are not correct; the calendar 'knows' the correct date but your getters don't return the date parts of the calendar. Also your setters don't make sense (they set their parameter which is lost after the method is finished).

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: printing GregorianCalendar default constructor?

    ahaaaaaaaaa

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: printing GregorianCalendar default constructor?

    Quote Originally Posted by IanKW View Post
    ahaaaaaaaaa
    Did you experience your Eureka moment?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    IanKW is offline Member
    Join Date
    Sep 2014
    Posts
    21
    Rep Power
    0

    Default Re: printing GregorianCalendar default constructor?

    I did! I really appreciate the help!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: printing GregorianCalendar default constructor?

    Good and you're welcome of course; if you look back at your code, you'll see that your class has a Calendar as well as a day, month and year variable; your Calendar 'knows' the day, month and year so your class doesn't need those variables (you have to keep them in sync with the Calendar, they're only a burden); your getters should return whatever the Calendar returns (e.g. day, month and year) and your setters should set the corresponding value in the Calendar object. The Calendar object itself should indeed be private because it's of no business to the users of your class.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. How to display default constructor values?
    By IanKW in forum New To Java
    Replies: 3
    Last Post: 09-14-2014, 10:25 AM
  2. Replies: 5
    Last Post: 08-11-2012, 09:50 AM
  3. Replies: 3
    Last Post: 11-01-2011, 08:24 AM
  4. Default constructor by compiler.
    By manishdivs in forum New To Java
    Replies: 2
    Last Post: 09-19-2011, 05:31 PM
  5. Printing default Swing values
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 11:09 AM

Posting Permissions

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