Pages

Compare current date with user input date in java

Complete source code below will show you, how to compare current date with user input date in java.

***************************************************************************
COMPLETE SOURCE CODE FOR : GetDate.java
***************************************************************************


import java.util.Date;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class GetDate implements ActionListener
{
int year=0;
int month=0;
int date=0;

String [] yearContent={"2000","2001","2002","2003","2004","2005","2006","2007","2008","2009","2010","2011"};
String [] monthContent={"January","February","March","April","Mei","Jun","July","August","September","October","November","December"};
String [] dateContent={"1","2","3","4","5","6","7","8","9","10",
"11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30",
"31"};

JComboBox yearComboBox=new JComboBox(yearContent);
JComboBox monthComboBox=new JComboBox(monthContent);
JComboBox dateComboBox=new JComboBox(dateContent);

JButton button=new JButton("Click here to compare to current date");

JFrame frame=new JFrame("Compare date with current date");

public GetDate()
{
button.addActionListener(this);

Box box=Box.createHorizontalBox();
box.add(dateComboBox);
box.add(monthComboBox);
box.add(yearComboBox);

frame.setLayout(new GridLayout(2,1));

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(box);
frame.add(button);
frame.setSize(500,100);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public void actionPerformed(ActionEvent event)
{
if(event.getSource()==button)
{
String tempDate=(String)dateComboBox.getSelectedItem();
date=Integer.parseInt(tempDate);

month=monthComboBox.getSelectedIndex();

String tempYear=(String)yearComboBox.getSelectedItem();
year=Integer.parseInt(tempYear)-1900;

Date currentDateValue=new Date();
Date userChooserDate=new Date(year,month,date);

int currentYear=currentDateValue.getYear();
int currentMonth=currentDateValue.getMonth();
int currentDate=currentDateValue.getDate();

if((date==currentDate)&&(month==currentMonth)&&(year==currentYear))
{
JOptionPane.showMessageDialog(frame,"Same date","SAME",JOptionPane.INFORMATION_MESSAGE);
}
else if(userChooserDate.compareTo(currentDateValue)<0)>0)
{
JOptionPane.showMessageDialog(frame,"Welcome to the future","FUTURE",JOptionPane.INFORMATION_MESSAGE);
}
}
}

public static void main(String[]args)
{
GetDate gd=new GetDate();
}
}


***************************************************************************
JUST COMPILE AND EXECUTE IT
***************************************************************************
Related Posts Plugin for WordPress, Blogger...