Hi, i am making an event calender but i'm stuck with making sure that a user choose a valid date (a user should'nt be able to choose a start date that is after the end date). Could anyone point me in the right direction please? My code is still a bit rough but i included it, if anyone want to have a better understanding.

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.GregorianCalendar;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


public class TestarDat extends javax.swing.JFrame {
	private JComboBox stDag;
	private JComboBox stAr;
	private JComboBox slDag;
	private JComboBox slTim;
	private JComboBox slAr;
	private JComboBox slMan;
	private JComboBox stTim;
	private JComboBox stMan;
	//private static String[] stDagar, stMånader, stÅr, slDagar, slMånader, slÅr;
	private static int år, mån, dag;
	
	private static DefaultComboBoxModel 
	stDagModel = new DefaultComboBoxModel(), 
	slDagModel= new DefaultComboBoxModel(), 
	stManModel= new DefaultComboBoxModel(), 
	slManModel= new DefaultComboBoxModel(), 
	stArModel= new DefaultComboBoxModel(), 
	slArModel= new DefaultComboBoxModel();
	
	
		public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				TestarDat inst = new TestarDat();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public TestarDat() {
		super();
		setBackground(new Color(199,226,233));
		
		GregorianCalendar cal = new GregorianCalendar();
		år = cal.get(GregorianCalendar.YEAR);
		mån = cal.get(GregorianCalendar.MONTH);
		dag = cal.get(GregorianCalendar.DAY_OF_MONTH);
		
		setDagar(år, mån, dag, år, mån , dag);
		setMånader(år, mån, år , mån);
		setÅr(år, år);
		initGUI();

		
	}
	
	private void initGUI() {
		try {
			GridBagLayout thisLayout = new GridBagLayout();
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			thisLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.1};
			thisLayout.rowHeights = new int[] {75, 42, 47, 7};
			thisLayout.columnWeights = new double[] {0.0, 0.0, 0.0, 0.1};
			thisLayout.columnWidths = new int[] {134, 140, 134, 7};
			getContentPane().setLayout(thisLayout);
			{
			
				stDag = new JComboBox();
				getContentPane().add(stDag, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				stDag.setModel(stDagModel);
			}
			{
				
				stMan = new JComboBox();
				getContentPane().add(stMan, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				stMan.setModel(stManModel);
				stMan.addActionListener(new stArLys());
				stMan.addActionListener(new stArLys());
			}
			{
				
				stAr = new JComboBox();
				getContentPane().add(stAr, new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				stAr.setModel(stArModel);
				stAr.addActionListener(new stArLys());
			}
			{
				DefaultComboBoxModel stTimModel = 
					new DefaultComboBoxModel(
							new String[] { "00:00", "01:00", "02:00" });
				stTim = new JComboBox();
				getContentPane().add(stTim, new GridBagConstraints(3, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				stTim.setModel(stTimModel);
			}
			{
				
				slDag = new JComboBox();
				getContentPane().add(slDag, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				slDag.setModel(slDagModel);
			}
			{
				
				slMan = new JComboBox();
				getContentPane().add(slMan, new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				slMan.setModel(slManModel);
			}
			{
			
				slAr = new JComboBox();
				getContentPane().add(slAr, new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				slAr.setModel(slArModel);
			}
			{
				DefaultComboBoxModel slTimModel = 
				new DefaultComboBoxModel(
							new String[] { "00:00", "01:00", "02:00" });
				slTim = new JComboBox();
				getContentPane().add(slTim, new GridBagConstraints(3, 2, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
				slTim.setModel(slTimModel);
			}
			pack();
			this.setSize(579, 300);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	public void setDagar(int stye, int stmon, int stday, int slye, int slmon, int slday)
	{
		if((stDagModel.getSize() != 0) && (slDagModel.getSize() != 0))
		{
		stDagModel.removeAllElements();
		slDagModel.removeAllElements();
		}
		
		GregorianCalendar stcal = new GregorianCalendar(stye, stmon, stday);
		GregorianCalendar slcal = new GregorianCalendar(slye, slmon, 1);
		int stdagsize = (stcal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH) - stday) + 1;
		int sldagsize = (slcal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH) - slday) + 1;

		if(stye == slye)
			if(stmon == slmon)
				for(int i = 0 ; i < stdagsize ; i++)
				{
					stDagModel.addElement(Integer.toString(stday + i));
					slDagModel.addElement(Integer.toString(stday + i));
				}
			
		if(stmon != slmon)
		{
			for(int i = 0 ; i < stdagsize ; i++)
				stDagModel.addElement(Integer.toString(stday + i));
			for(int i = 0 ; i < sldagsize ; i++)
				slDagModel.addElement(Integer.toString(stday + i));
		}
	}
	
	public void setMånader(int stye, int stmon, int slye, int slmon)
	{
		stManModel.removeAllElements();
		slManModel.removeAllElements();
		
		GregorianCalendar stcal =  new GregorianCalendar();
		GregorianCalendar slcal =  new GregorianCalendar(stye, stmon, 1); 
		int stmansize = (stcal.getActualMaximum(GregorianCalendar.MONTH) - mån) + 1;
		int slmansize = (slcal.getActualMaximum(GregorianCalendar.MONTH) - mån) + 1;

		
		if(stye == år)
			for(int i = 0 ; i < stmansize ; i++)
				stManModel.addElement(Integer.toString((stmon + i) +1));
		
		if(stye > år)
			for(int i = 0 ; i < 12 ; i++)
				stManModel.addElement(Integer.toString(1 + i));
		
		if(slye == stye)
			for(int i = 0 ; i < slmansize ; i++)
				slManModel.addElement(Integer.toString((slmon + i) +1));
		
		else
			for(int i = 0 ; i < slmansize ; i++)
				slManModel.addElement(Integer.toString((stmon + i)) +1);
		
	}
	
	public void setÅr(int stye, int slye)
	{
		stArModel.removeAllElements();
		slArModel.removeAllElements();
		
		stArModel.addElement(Integer.toString(år));
		stArModel.addElement(Integer.toString(år +1));
		
		if(stye <= år)
		{
			stArModel.setSelectedItem(Integer.toString(år));
			slArModel.addElement(Integer.toString(år));
			slArModel.addElement(Integer.toString(år +1));
		}
        
        else if (stye > år)
        {
            stArModel.setSelectedItem(Integer.toString(år+1));
            slArModel.addElement(Integer.toString(år+1));
            }

		else
		{
			slArModel.addElement(Integer.toString(stye));
			stArModel.setSelectedItem(Integer.toString(år +1));
		}
			
	}

	class stArLys implements ActionListener 
	{ 
		public void actionPerformed(ActionEvent e) 
		{
			
			int startÅr = Integer.parseInt((String) stAr.getSelectedItem());
			int slutÅr = Integer.parseInt((String) slAr.getSelectedItem());
			int startMån = Integer.parseInt((String) stMan.getSelectedItem());
			int slutMån = Integer.parseInt((String) slMan.getSelectedItem());

			setÅr(startÅr, slutÅr);
			setMånader(startÅr, startMån, slutÅr, slutMån);
			//setDagar(start, mån, dag, slut, mån, dag);
			

		}
	}
	
	class stManLys implements ActionListener 
	{ 
		public void actionPerformed(ActionEvent e) 
		{
			
			int start = Integer.parseInt((String) stMan.getSelectedItem());
			int slut = Integer.parseInt((String) slMan.getSelectedItem());
			
			setÅr(start, slut);
			setMånader(start, mån, slut, mån);
			setDagar(start, mån, dag, slut, mån, dag);
			
		}
	}


}

Without looking at your code, (gotta be somewhere soon) I'd recommend that you use compareTo to compare the two Dates to each other. If you aren't using the Date class to store your dates, look at the compareTo method for whatever class you're using

GregorianCalendar inherits getTimeInMillis() method from Calendar class. Any ideas how you can use this to your advantage? ;)

Thanks, im not familiar with these methods but i'll have a look at them.

GregorianCalendar inherits getTimeInMillis() method from Calendar class. Any ideas how you can use this to your advantage? ;)

I've been looking at getTimeInMillis() but im not quite sure what you meant. Can i use getTimeInMillis() to make sure end date is bigger than start date?

Can i use getTimeInMillis() to make sure end date is bigger than start date?

Exactly

Exactly

Could you maybe explain it a bit better, im having a tough time understanding it.

Have look on code bellow, but by checking your code (just few minutes ago) I guess second part with SimpleDateFormat would be more suitable your case

import java.util.Calendar;
 import java.util.GregorianCalendar;
 import java.util.Date;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;


public class DateComparison {

    public static void main(String[] args) {
    	
    	GregorianCalendar date1 = new GregorianCalendar(2009,4,11,12,30);
    	System.out.println(date1.getTime().toString()+ " as long type "+date1.getTimeInMillis());
    	GregorianCalendar date2 = new GregorianCalendar(2009,4,15,12,30);
    	System.out.println(date2.getTime().toString()+ " as long type "+date2.getTimeInMillis()+"\n");
    	
    	DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");
    	String strDate1 = "11/05/2009 12:30";
    	Calendar cal1 = Calendar.getInstance();
    	
    	try{
    		Date d1 = df.parse(strDate1);
    		cal1.setTime(d1);
    	}
    	catch(ParseException pe){
    		System.out.println("Unable to parse");
    	}
    	System.out.println(cal1.getTime().toString());    		    		   	
    } 
}

peter - I looked at the compareTo method, it does the same as getTimeInMillis basically, except it compares the two times in millis for you.

OP - if you did firstDate.compareTo(secondDate) it would return a value indicating which one was greater. Have a look at the documentation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.