I was in the mood for doing some GUI coding, and eventually I came with this simple program: you enter a date, press the button, and the program tells you temporal difference in days between the input date and current date.
package net.coderodde.gui.date;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class DaysCounter implements ChangeListener, ActionListener {
/**
* The actual frame of this program.
*/
private final JFrame frame;
/**
* The label displaying the currently selected day of month.
*/
private final JLabel dayLabel;
/**
* The slider for choosing the day of month.
*/
private final JSlider daySlider;
/**
* The combo box for choosing the month.
*/
private final JComboBox monthComboBox;
/**
* The text field for inputting the year.
*/
private final JTextField yearTextField;
/**
* The button for initiating the age calculation.
*/
private final JButton computeButton;
/**
* Constructs the graphical user interface.
*/
public DaysCounter() {
// Construct components:
this.frame = new JFrame("Age in days");
this.dayLabel = new JLabel("16");
this.daySlider = new JSlider(1, 31, 16);
String[] months = new String[] {
"January",
"February",
"March",
"April",
"May",
"June",
"Jule",
"August",
"September",
"October",
"November",
"December",
};
this.monthComboBox = new JComboBox(months);
this.yearTextField = new JTextField("1980");
this.computeButton = new JButton("Compute");
// Add listeners:
daySlider.addChangeListener(this);
computeButton.addActionListener(this);
// Align the selected day field horizontally.
dayLabel.setHorizontalAlignment(JLabel.CENTER);
// Add the components to the frame.
frame.getContentPane().setLayout(new GridLayout(5, 1));
frame.getContentPane().add(dayLabel);
frame.getContentPane().add(daySlider);
frame.getContentPane().add(monthComboBox);
frame.getContentPane().add(yearTextField);
frame.getContentPane().add(computeButton);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
// Center out the frame.
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation((dim.width - frame.getWidth()) / 2,
(dim.height - frame.getHeight()) / 2);
frame.setVisible(true);
}
public static void main(final String... args) {
javax.swing.SwingUtilities.invokeLater(() -> {
new DaysCounter();
});
}
/**
* Change the current day of month.
*
* @param e the event object.
*/
@Override
public void stateChanged(ChangeEvent e) {
dayLabel.setText("" + ((JSlider) daySlider).getValue());
}
/**
* Initiates the age calculation.
*
* @param e the button press event, ignored.
*/
@Override
public void actionPerformed(ActionEvent e) {
Calendar start = Calendar.getInstance();
Calendar end = Calendar.getInstance();
int year;
try {
year = Integer.parseInt(yearTextField.getText());
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(
frame,
"Bad year: " + yearTextField.getText(),
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}
start.set(year,
monthComboBox.getSelectedIndex(),
daySlider.getValue());
if (!isValidDate(start)) {
JOptionPane.showMessageDialog(null,
"The input date is invalid!",
"Error",
JOptionPane.ERROR_MESSAGE);
return;
}
Date startDate = start.getTime();
Date endDate = end.getTime();
long startTime = startDate.getTime();
long endTime = endDate.getTime();
long days = (endTime - startTime) / (1000 * 60 * 60 * 24);
JOptionPane.showMessageDialog(
frame,
"You are " + days + " day" + (days == 1 ? "" : "s") + " old.",
"",
JOptionPane.INFORMATION_MESSAGE);
}
/**
* Checks the validity of the input date.
*
* @param c the input date.
* @return {@code true} only if the input date is valid.
*/
private boolean isValidDate(Calendar c) {
c.setLenient(false);
try {
c.get(Calendar.DAY_OF_MONTH);
c.get(Calendar.MONTH);
c.get(Calendar.YEAR);
return true;
} catch (Exception ex) {
return false;
}
}
}
So, what do you think?