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

Thread: Gregorian Calendar - Problems

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Gregorian Calendar - Problems

    Hello... I am working on a project for my intro to Java class and I am stuck. We have to design a functioning calendar, and I am using GregorianCalendar to pull my Current Day, Month, Year, etc. For some reason the month is off by 1 month though (6 instead of 7). Also, I would much rather return the name of the month (July instead of 6) but I can't seem to figure it out. Lastly, I can't seem to get the number of days to fall on the buttons in the proper place. I know I'm asking a lot, but this assignment is really hard, and I'm trying to get as much help as I can because I am stuck...

    Here is my code...

    public class Calendar extends JFrame {
     
    	public Calendar() {
    		this.setSize(640,480);
    		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		this.setVisible(true);
    		this.setTitle("Calendar");
     
    		JPanel borderPanel = new JPanel();
    		borderPanel.setLayout(new BorderLayout());
    		getContentPane().add(borderPanel);
     
    		// Initialize GregorianCalendar
    		GregorianCalendar cal = new GregorianCalendar();
    		int currYear = cal.get(GregorianCalendar.YEAR);
    		int currDOM = cal.get(GregorianCalendar.DAY_OF_MONTH);
    		int currMnth = cal.get(GregorianCalendar.MONTH);
    		int nod, som;
    		nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH); //Number of Days
    		som = cal.get(GregorianCalendar.DAY_OF_WEEK); // Start of the Month
     
    		// Button Layout
    		final int ROWS = 6;
    		final int COLS = 7;
    		JButton[][] days;
    		JPanel calendar = new JPanel();
    		calendar.setLayout(new GridLayout(6,7));
    		days = new JButton[ROWS][COLS];
    		for(int row = 0; row < ROWS; row++) {
    			for(int col = 0; col < COLS; col++) {
    				days[row][col] = new JButton("");
    				calendar.add(days[row][col]);
    			}
    		}
     
    		// Add Some Number to the Buttons
    		int[] tableMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
    		int startDay = som;
    		int daysInMonth = tableMonth[currMnth];
    		for(int i = 0; i < daysInMonth; i++){
    			days[(startDay + i)/7][(startDay + i) % 7].setText("" + (i + 1));
    		}
    		borderPanel.add(calendar, BorderLayout.CENTER);
     
    		// North Panel - Current Month / Prev & Next Button
    		JPanel month = new JPanel();	
    		month.add(new JButton("Previous"));
    		month.add(new JButton("The Month is: " + currMnth));
    		month.add(new JButton("Next"));
    		borderPanel.add(month, BorderLayout.NORTH);
     
    		// West Panel - Current Date
    		JPanel year = new JPanel();
    		year.add(new JButton("The Year is: " + currYear ));
    		borderPanel.add(year, BorderLayout.SOUTH);
     
    		// South Panel - Current Year
    		JPanel today = new JPanel();
    		today.add(new JButton("Today is: " + currMnth + "/" + currDOM + "/" + currYear));
    		borderPanel.add(today, BorderLayout.WEST);
     
    	}
    	public static void main(String[] args) {
    		new Calendar();
    	}
    }

    Thank you so much for any help!


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Gregorian Calendar - Problems

    As for the name of the month, a simple switch statement should do it

    As for the month thing, I'm getting that too.

    I think that it is supposed to return 6.

    Constant Field Values (Java Platform SE 7 )

    January is 0.

    int month = cal.get(GregorianCalendar.MONTH);

    String month2 = "";

    switch (month)
    {

    case 0:
    month2 = "January";
    break;
    case 1:
    month2 = "February";
    break;
    case 2:
    month2 = "March";
    break;
    etc
    }

    Quote Originally Posted by BobDole6395
    Lastly, I can't seem to get the number of days to fall on the buttons in the proper place.
    What do you mean exactly by that?
    Last edited by GoodbyeWorld; July 3rd, 2012 at 12:14 AM.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Gregorian Calendar - Problems

    What do you mean exactly by that?
    The days of the month don't fall on the proper blocks. For example, July first was a Sunday, but it is falling on Tuesday. I think something is wrong with my for loop, but I can't see it.

  4. #4
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Gregorian Calendar - Problems

    cal.get(GregorianCalendar.DAY_OF_WEEK);

    is returning 3 for today.


    I assume you meant to get the start day of the month.

    Maybe you should use

    cal.get(GregorianCalendar.DAY_OF_WEEK_IN_MONTH);

    for figuring out the day_of_week/day_in_month correspondence.



    public static final int DAY_OF_WEEK_IN_MONTH
    Last edited by GoodbyeWorld; July 3rd, 2012 at 12:57 AM.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Gregorian Calendar - Problems

    Thanks so much! I got the month to return with July instead of 6 thanks to the switch. I am not quite getting the calendar to fill in properly, I tried the DAY_OF_WEEK_IN_MONTH but it didn't help, or I did it wrong. I swear this stuff is way to hard for a 100 level intro to Java, but I gotta get it done, lol... Thanks again!

  6. #6
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Gregorian Calendar - Problems

    Okay... so I've got everything working in terms of populating the buttons with the calendar. I am trying to setup the ActionListener for the prev and next buttons and I can't get it to work... well, its working, if I hit the button and do a println it updates the month +1 or -1 in my console, but the calendar itself is not refreshing. So, any ideas would be greatly appreciated!!!

    public class Calendar extends JFrame {
     
    	static JButton btnNext, btnPrev;
    	static int currYear, currDOM, currentMonth;
     
    	public Calendar() {
    		this.setSize(800,600);
    		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    		this.setVisible(true);
    		this.setTitle("Calendar");
     
     
    		JPanel borderPanel = new JPanel();
    		borderPanel.setLayout(new BorderLayout());
    		getContentPane().add(borderPanel);
     
    		// Initialize GregorianCalendar
    		GregorianCalendar cal = new GregorianCalendar();
    		int currYear = cal.get(GregorianCalendar.YEAR);
    		int currDOM = cal.get(GregorianCalendar.DAY_OF_MONTH);
    		int currMnth = cal.get(GregorianCalendar.MONTH);
    		currentMonth = currMnth;
    		String StringMnth = "";
    			switch(currMnth) {
    			case 0: StringMnth = "January"; break;
    			case 1: StringMnth = "February"; break;
    			case 2: StringMnth = "March"; break;
    			case 3: StringMnth = "April"; break;
    			case 4: StringMnth = "May"; break;
    			case 5: StringMnth = "June"; break;
    			case 6: StringMnth = "July"; break;
    			case 7: StringMnth = "August"; break;
    			case 8: StringMnth = "September"; break;
    			case 9: StringMnth = "October"; break;
    			case 10: StringMnth = "November"; break;
    			case 11: StringMnth = "December"; break;
    		}
    		int nod, som;
    		cal.set(GregorianCalendar.DATE, 1);
    		nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH); //Number of Days
    		som = (cal.get(GregorianCalendar.DAY_OF_WEEK) - GregorianCalendar.SUNDAY + 7)%7; // Start of the Month
     
    		// Button Layout
    		final int ROWS = 6;
    		final int COLS = 7;
    		JButton[][] days;
    		JPanel calendar = new JPanel();
    		calendar.setLayout(new GridLayout(6,7));
    		days = new JButton[ROWS][COLS];
    		for(int row = 0; row < ROWS; row++) {
    			for(int col = 0; col < COLS; col++) {
    				days[row][col] = new JButton("");
    				calendar.add(days[row][col]);
    			}
    		}
     
    		// Add Some Number to the Buttons
    		int[] tableMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
    		int startDay = som;
    		int daysInMonth = tableMonth[currMnth];
    		for(int i = 0; i < daysInMonth; i++){
    			days[(startDay + i)/7][(startDay + i) % 7].setText("" + (i + 1));
    		}
    		borderPanel.add(calendar, BorderLayout.CENTER);
     
    		// North Panel - Current Month / Prev & Next Button
    		btnNext = new JButton(">>");
    		btnPrev = new JButton("<<");
    		JPanel month = new JPanel();	
    		month.add(btnPrev);
    		month.add(new JLabel(StringMnth));
    		month.add(btnNext);
    		borderPanel.add(month, BorderLayout.NORTH);
     
    		//Listeners
    		btnPrev.addActionListener(new btnPrev_Action());
    		btnNext.addActionListener(new btnNext_Action());
     
    		// West Panel - Current Date
    		JPanel year = new JPanel();
    		year.add(new JButton("The Year is: " + currYear ));
    		borderPanel.add(year, BorderLayout.SOUTH);
     
    		// South Panel - Current Year
    		JPanel today = new JPanel();
    		today.add(new JButton("Today is: " + StringMnth + " " + currDOM + ", " + currYear));
    		borderPanel.add(today, BorderLayout.WEST);
     
    	}
     
    	static class btnNext_Action implements ActionListener{
    		public void actionPerformed (ActionEvent e){
    			currentMonth += 1;
    			System.out.print(currentMonth);
    		}
    	}
     
    	static class btnPrev_Action implements ActionListener{
    		public void actionPerformed (ActionEvent e){
    			currentMonth -= 1;
    			System.out.print(currentMonth);
    		}
    	}
    	public static void main(String[] args) {
    		new Calendar();
    	}
    }

  7. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Gregorian Calendar - Problems

    Anyone? I am stuck on the ActionListener... I can't seem to figure this out and it's driving me nuts!

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

    Default Re: Gregorian Calendar - Problems

    Changing currentMonth will not trigger the GUI to recreate the calendar. You will need to figure out a way to recreate the GUI when you change the month (perhaps by moving some of the GUI creation code into a method that you can call from the actionListener).
    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. Replies: 9
    Last Post: March 16th, 2011, 11:35 AM
  2. [SOLVED] Weird calendar.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 3rd, 2011, 08:19 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