Getting the last day of each month

Everything works except one month

Page 1 of 1

6 Replies - 11627 Views - Last Post: 01 April 2009 - 12:17 PM Rate Topic: -----

#1 happycamper  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 163
  • Joined: 11-November 08

Getting the last day of each month

Posted 30 March 2009 - 02:06 PM

monthCombo.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				dayCombo.removeAllItems();
				GregorianCalendar c = new GregorianCalendar();
				
				// set month to the month the user selected
				// 0 = Jan, 1 = Feb, etc.. 
				c.set(Calendar.MONTH, monthCombo.getSelectedIndex());

				// this line gets the last day of the month
				int lastDay = c.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);

				// just testing the output
				JOptionPane.showMessageDialog(null, c.getActualMaximum(GregorianCalendar.DAY_OF_MONTH));

				// populate the dayCombo box with each day in the month
				for (int i = 1; i <= lastDay; i++)
					dayCombo.addItem(new Integer(i));
			}
		});



So there are two combo boxes in my program. One for the month and one for the day. I have it so when the user selects the month the next combo box is populated with the days in that month. Above is the actionlistener for the month combo box that populates the dayCombo box.

Here is the weird thing. It gives me the the correct last day for every month.. except it gives me 31 days for February.

Is This A Good Question/Topic? 0
  • +

Replies To: Getting the last day of each month

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,533
  • Joined: 05-May 05

Re: Getting the last day of each month

Posted 30 March 2009 - 02:43 PM

That's really weird..

This works though, good luck

import java.util.Calendar;
import java.util.GregorianCalendar;

class Test
{
	public static void main(String... cmd)
	{
		Calendar c = GregorianCalendar.getInstance();
		c.set(Calendar.MONTH, Calendar.JANUARY);
		System.out.println(c.getActualMaximum(Calendar.DAY_OF_MONTH));
		c.add(Calendar.MONTH, 1);
		System.out.println(c.getActualMaximum(Calendar.DAY_OF_MONTH));
	}
	
}

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Getting the last day of each month

Posted 30 March 2009 - 05:36 PM

Show all your code
you must tweak something somewhere else
Was This Post Helpful? 0
  • +
  • -

#4 happycamper  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 163
  • Joined: 11-November 08

Re: Getting the last day of each month

Posted 30 March 2009 - 08:36 PM

I'm starting to think it is a bug within the GregorianCalendar class. Here is my code below. You'll notice I only interact with the calendar in two spots. One of them in the actionlistener we've already looked at and another time in the actionlistener for a button. Since we're not pressing the button that section of the code is irrelevant to the problem. To further prove that point I commented out that actionlistener for that button and the program behaved the exact same way.

So, knowing that the only thing that effects the calendar at this point is the actionlistener for the combo box, I tried to further isolate the problem. I added some message boxes to show the output. Here is the puzzling part. If you look below, I have a box to show the index selected from the monthCombo box. This shows the exact output I expect every time. 0 for January, 1 for February and so on. I added another messagebox to show the month the GregorianCalendar object was set to. I got the exact output I expected every time.. except once. When the index is 1 for Feb.. for some reason it sets the month to 2 everytime!!

To further illustrate the phenomena I changed the line:
				 // set month to the month the user selected
				c.set(Calendar.MONTH, monthCombo.getSelectedIndex());



to
// set month to the month the user selected
				c.set(Calendar.MONTH, 1);



..and it still sets the month to 2 (March)!! Which explains why the lastDay is set to 31, because there are infact 31 days in March.

Conclusion: I have no idea what the hell is going on!!

The actionlistener code:

monthCombo = new JComboBox();
		monthCombo.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				dayCombo.removeAllItems();
				GregorianCalendar c = new GregorianCalendar();

				// set month to the month the user selected
				c.set(Calendar.MONTH, monthCombo.getSelectedIndex());

				// get last day of the month
				int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
				JOptionPane.showMessageDialog(null, "Index: " + monthCombo.getSelectedIndex());
				JOptionPane.showMessageDialog(null, "Month: " + c.get(Calendar.MONTH));
				JOptionPane.showMessageDialog(null, "lastDay: " + lastDay);
				
				for (int i = 1; i <= lastDay; i++)
					dayCombo.addItem(new Integer(i));
			}
		});



The entire class:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class BirthdayFrame extends JFrame
{
	private JComboBox dayCombo;
	private JComboBox monthCombo;
	
	public BirthdayFrame()
	{
		setTitle("Birthday Calendar");
		setSize(250, 200);

		GridBagLayout layout = new GridBagLayout();
		setLayout(layout);

		JLabel dayLabel = new JLabel("Day: ");
		JLabel monthLabel = new JLabel("Month: ");

		dayCombo = new JComboBox();

		// create combo box and populate it with the months of a year
		monthCombo = new JComboBox();
		monthCombo.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				dayCombo.removeAllItems();
				GregorianCalendar c = new GregorianCalendar();

				// set month to the month the user selected
				c.set(Calendar.MONTH, monthCombo.getSelectedIndex());

				// get last day of the month
				int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
				JOptionPane.showMessageDialog(null, "Index: " + monthCombo.getSelectedIndex());
				JOptionPane.showMessageDialog(null, "Month: " + c.get(Calendar.MONTH));
				JOptionPane.showMessageDialog(null, "lastDay: " + lastDay);
				
				for (int i = 1; i <= lastDay; i++)
					dayCombo.addItem(new Integer(i));
			}
		});

		String months[] = {"January", "February", "March", "April", "May",
			"June", "July", "August", "September", "October", "November", "December" };

		for (int i = 0; i < months.length; i++)
			monthCombo.addItem(months[i]);


		
		JButton button = new JButton("PUSH");
		button.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				int month = monthCombo.getSelectedIndex();
				int birthday = dayCombo.getSelectedIndex() + 1;
				PrintCalendar c = new PrintCalendar(month, birthday);
				c.makeCalendar();
				JOptionPane.showMessageDialog(null, c.getCalendarString(), 
						"Your Next Birthday", JOptionPane.INFORMATION_MESSAGE);
			}
		});

		JButton instructions = new JButton("Instructions");
		instructions.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				JOptionPane.showMessageDialog(null, "Enter your birthdate.\n" +
					"The program will print a calendar of your " +
					"next birthday.\n" +
						"This is a test program for the PrintCalendar class I made.\n " +
						"The point of the class is to be able to print out a calendar.\n " +
						"Depending on which constructor is chosen, it will print out today's date or a date specified in the future.", "Instructions", JOptionPane.INFORMATION_MESSAGE);
			}
		});
	   
		// add components to grid
		GridBagConstraints constraints = new GridBagConstraints();

		constraints.weightx = 100;
		constraints.weighty = 100;

		// add monthLabel
		constraints.gridx = 0;
		constraints.gridy = 0;
		add(monthLabel, constraints);

		// add dayTextBox
		constraints.gridx = 1;
		constraints.gridy = 0;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		add(monthCombo, constraints);

		// add monthLabel
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.fill = GridBagConstraints.NONE;
		add(dayLabel, constraints);

		// monthTextBox
		constraints.gridx = 1;
		constraints.gridy = 1;
		constraints.fill = GridBagConstraints.HORIZONTAL;
		add(dayCombo, constraints);

		// add button
		constraints.gridx = 0;
		constraints.gridy = 2;
		constraints.gridwidth = 1;
		constraints.gridheight = 1;
		constraints.fill = GridBagConstraints.CENTER;
		add(button, constraints);

		// add instructions
		constraints.gridx = 1;
		constraints.gridy = 2;
		constraints.gridwidth = 1;
		constraints.gridheight = 1;
		add(instructions, constraints);
	}
}


This post has been edited by happycamper: 30 March 2009 - 08:37 PM

Was This Post Helpful? 0
  • +
  • -

#5 happycamper  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 163
  • Joined: 11-November 08

Re: Getting the last day of each month

Posted 30 March 2009 - 08:59 PM

Okay so I just decided to change the way I did things. Instead of setting the month to the month the user selected by doing this:

c.set(Calendar.MONTH, monthCombo.getSelectedIndex());



I set it the the month the user selected in the constructor of the GregorianCalendar object:

GregorianCalendar c = new GregorianCalendar(Calendar.YEAR, monthCombo.getSelectedIndex(), 1);


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Getting the last day of each month

Posted 31 March 2009 - 05:49 PM

View Posthappycamper, on 30 Mar, 2009 - 07:59 PM, said:

Okay so I just decided to change the way I did things. Instead of setting the month to the month the user selected by doing this:

c.set(Calendar.MONTH, monthCombo.getSelectedIndex());



I set it the the month the user selected in the constructor of the GregorianCalendar object:

GregorianCalendar c = new GregorianCalendar(Calendar.YEAR, monthCombo.getSelectedIndex(), 1);


Not sure it is related to your problem
Will need you PrintCalendar class to test the whole thing
Still seriously doubt there is a bug in the GregorianCalendar class.... thousand of applications (including some I wrote) would have crashed over the world
Was This Post Helpful? 0
  • +
  • -

#7 happycamper  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 163
  • Joined: 11-November 08

Re: Getting the last day of each month

Posted 01 April 2009 - 12:17 PM

Okay now this is bizarre.

As I posted before, I got the code working now by setting the month in the GregorianCalendar constructor rather than using the set() method. But I went back to the old way I had it and now it works perfectly even with the old way that didn't work before.

I don't think showing you the PrintCalendar class, pbl, would do any good because the problem does not interact with the PrintCalendar class. I proved it by commenting out all objects of the PrintCalendar class in the BirthdayFrame class and the program still behaved the same way.

Oh well, either way, problem solved. Life is good. :^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1