This is my first GUI program in Java. If finds the day of the week for any given date which is after 1 January 1900.
Day of the week GUI
/** @author Georgi Christov ( a.k.a. 8masterofpuppets8 )
* @version 1.0
* @since 12.10.2009 */
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class DayOfWeek {
private String day;
private String month;
private String year;
public DayOfWeek( String d, String m, String y ) {
day = d; month = m; year = y;
}
private int changeMonth( String m ) {
if ( month.equalsIgnoreCase( "January" ) )
return 1;
else if ( month.equalsIgnoreCase( "February" ) )
return 2;
else if ( month.equalsIgnoreCase( "March" ) )
return 3;
else if ( month.equalsIgnoreCase( "April" ) )
return 4;
else if ( month.equalsIgnoreCase( "May" ) )
return 5;
else if ( month.equalsIgnoreCase( "June" ) )
return 6;
else if ( month.equalsIgnoreCase( "July" ) )
return 7;
else if ( month.equalsIgnoreCase( "August" ) )
return 8;
else if ( month.equalsIgnoreCase( "September" ) )
return 9;
else if ( month.equalsIgnoreCase( "October" ) )
return 10;
else if ( month.equalsIgnoreCase( "November" ) )
return 11;
else if ( month.equalsIgnoreCase( "December" ) )
return 12;
return Integer.parseInt( month );
}
public boolean isLeap( int y ) {
if ( y % 4 == 0 && y % 100 != 0 ) {
return true;
}
else if ( y % 4 == 0 && y % 100 == 0 ) {
if ( y % 400 == 0 ) return true;
}
return false;
}
private String calculateDayOfWeek( int day, int month, int year ) {
// Considering the fact that 01.01.1900 fell on a Monday
int startYear = 1900, startDay = 0, daysInCurrentMonth = 0;
int yearCounter = startYear, daysOfYear;
int totalDays = 0;
boolean leapYear = false;
String finalDay = "";
/* This first loop is used to calculate the years from 1900 up to but excluding the
input year */
while ( yearCounter <= year ) {
leapYear = false;
daysOfYear = 365;
// Check if the current year is a leap-year or not
if ( yearCounter % 4 == 0 && yearCounter % 100 != 0 ) {
leapYear = true;
}
else if ( yearCounter % 4 == 0 && yearCounter % 100 == 0 ) {
if ( yearCounter % 400 == 0 ) leapYear = true;
}
if ( leapYear ) daysOfYear = 366;
// This check is used because we don't need all days from the last year
if ( yearCounter < year ) totalDays += daysOfYear;
yearCounter += 1;
}
/* Here the program has reached the input year and need to add the missing
month values */
for ( int count = 1; count < month; count++ ) {
switch ( count ) {
case 1: daysInCurrentMonth = 31; break;
case 2: {
if ( leapYear ) daysInCurrentMonth = 29;
else daysInCurrentMonth = 28;
break;
}
case 3: daysInCurrentMonth = 31; break;
case 4: daysInCurrentMonth = 30; break;
case 5: daysInCurrentMonth = 31; break;
case 6: daysInCurrentMonth = 30; break;
case 7: daysInCurrentMonth = 31; break;
case 8: daysInCurrentMonth = 31; break;
case 9: daysInCurrentMonth = 30; break;
case 10: daysInCurrentMonth = 31; break;
case 11: daysInCurrentMonth = 30; break;
case 12: daysInCurrentMonth = 31; break;
}
totalDays += daysInCurrentMonth;
}
totalDays += day; // Add remaining days to the ones calculated so far
// Find the day of the week having in mind that 1.1.1900 fell on a Monday
if ( totalDays > 7 ) {
if ( totalDays % 7 != 0 ) startDay = totalDays % 7; // Where the remainder is the day needed
else startDay = 7; // In this case the day is always Sunday
}
else startDay = day;
if ( startDay == 1 ) finalDay = "Monday";
else if ( startDay == 2 ) finalDay = "Tuesday";
else if ( startDay == 3 ) finalDay = "Wednesday";
else if ( startDay == 4 ) finalDay = "Thursday";
else if ( startDay == 5 ) finalDay = "Friday";
else if ( startDay == 6 ) finalDay = "Saturday";
else if ( startDay == 7 ) finalDay = "Sunday";
return finalDay;
}
public String calculateDay() {
return calculateDayOfWeek( Integer.parseInt( day ), changeMonth( month ), Integer.parseInt( year ) );
}
}
public class ApplicationDemo extends JPanel implements ActionListener {
private static final long serialVersionUID = 1L;
JTextField resultField, yearField;
JFrame frame;
JPanel panel;
JButton calculateButton, exitButton, resetButton;
JLabel dLabel, mLabel, yLabel;
JComboBox dayValues, monthValues;
String d = "1", m = "January";
@SuppressWarnings( "deprecation" )
public ApplicationDemo() {
frame = new JFrame( "DayOfWeek" );
panel = new JPanel();
panel.setLayout( null );
String days[] = { "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" };
String months[] = { "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December" };
dayValues = new JComboBox( days ); dayValues.setBounds( 20, 25, 50, 24 );
dayValues.addActionListener( this ); dayValues.setBackground( Color.WHITE );
monthValues = new JComboBox( months ); monthValues.setBounds( 95, 25, 100, 24 );
monthValues.addActionListener( this ); monthValues.setBackground( Color.WHITE );
yearField = new JTextField(); yearField.setHorizontalAlignment( JTextField.CENTER );
yearField.setBounds( 220, 25, 50, 25 );
yearField.setToolTipText( "Enter year >= 1900." );
dLabel = new JLabel(); dLabel.setText( "Day" ); dLabel.setBounds( 33, 3, 30, 25 );
mLabel = new JLabel(); mLabel.setText( "Month" ); mLabel.setBounds( 125, 3, 35, 25 );
yLabel = new JLabel(); yLabel.setText( "Year" ); yLabel.setBounds( 230, 3, 33, 25 );
calculateButton = new JButton( "Calculate day of week" );
calculateButton.addActionListener( this );
exitButton = new JButton( "Exit" );
exitButton.addActionListener( this );
resetButton = new JButton( "Reset" );
resetButton.addActionListener( this );
exitButton.setBounds( 150, 130, 80, 25 );
calculateButton.setBounds( 65, 60, 160, 25 );
resetButton.setBounds( 60, 130, 80, 25 );
calculateButton.setToolTipText( "Calculate day of the week." );
exitButton.setToolTipText( "Close program." );
resetButton.setToolTipText( "Reset all fields." );
resultField = new JTextField();
resultField.setHorizontalAlignment( JTextField.CENTER );
resultField.setBounds( 45, 95, 200, 25 );
resultField.setEditable( false );
panel.add( yearField ); panel.add( dayValues ); panel.add( monthValues );
panel.add( calculateButton ); panel.add( resultField );
panel.add( exitButton ); panel.add( resetButton );
panel.add( dLabel ); panel.add( mLabel ); panel.add( yLabel );
frame.setContentPane( panel );
frame.setBounds( 470, 200, 300, 200 );
frame.setDefaultCloseOperation( 0 );
frame.show();
}
public void actionPerformed( ActionEvent e ) {
if ( e.getSource() == calculateButton ) {
DayOfWeek classObject = new DayOfWeek( d, m, yearField.getText() );
String result = "";
try {
if ( !classObject.isLeap( Integer.parseInt( yearField.getText() ) ) &&
Integer.parseInt( d ) > 28 && m.equals( "February" ) )
resultField.setText( "Year is not leap!" );
else if ( Integer.parseInt( yearField.getText() ) < 1900 )
resultField.setText( "Year must be >= 1900." );
else {
result = classObject.calculateDay();
resultField.setText( "Fell/falls on a " + result );
}
} catch ( NumberFormatException exc ) {
result = "Only numerical data please!";
resultField.setText( result );
}
}
else if ( e.getSource() == exitButton ) System.exit( 0 );
else if ( e.getSource() == resetButton ) {
resultField.setText( "" );
yearField.setText( "" );
d = "1"; m = "January";
dayValues.setSelectedIndex( 0 );
monthValues.setSelectedIndex( 0 );
}
else if ( e.getSource() == dayValues ) {
d = ( String ) dayValues.getSelectedItem();
}
else if ( e.getSource() == monthValues ) {
m = ( String ) monthValues.getSelectedItem();
}
}
public static void main( String[] args ) {
new ApplicationDemo();
}
}
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.