2013年6月18日 星期二

輸入西元年.月份.幾號就能查出這天是星期幾

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

class MyJFrame extends JFrame implements ActionListener{
private JPanel contentPane;
private JTextField txtyear, txtmonth, txtdate,txtresult;
private JLabel lblyear, lblmonth, lbldate,lblresult;
private JButton Pressbutton;

 MyJFrame(){

     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     setBounds(100, 100, 600, 400);
     contentPane = new JPanel();
     setContentPane(contentPane);
     contentPane.setLayout(null);

    lbldate=new JLabel("Please input date");
    lbldate.setBounds(20,80,150,25);
    contentPane.add(lbldate);
    txtdate = new JTextField();
    txtdate.setColumns(10);
    txtdate.setBounds(20, 100, 150, 25);
     contentPane.add(txtdate);
    
     lblmonth=new JLabel("Please input month");
     lblmonth.setBounds(200,80,150,25);
     contentPane.add(lblmonth);
     txtmonth = new JTextField();
     txtmonth.setColumns(20);
     txtmonth.setBounds(200, 100, 150, 25);
     contentPane.add(txtmonth);
   
   lblyear=new JLabel("Please input year");
   lblyear.setBounds(420,80,150,25);
   contentPane.add(lblyear);
   txtyear = new JTextField();
   txtyear.setColumns(10);
   txtyear.setBounds(420, 100, 150, 25);
   contentPane.add(txtyear);
  
  
   lblresult=new JLabel("The day of result");
   lblresult.setBounds(200,140,150,25);
   contentPane.add(lblresult);
   txtresult = new JTextField();
   txtresult.setColumns(10);
   txtresult.setBounds(200, 160, 150, 25);
   contentPane.add(txtresult);
  
   Pressbutton=new JButton("Please press me");
   Pressbutton.addActionListener(this);
   Pressbutton.setBounds(20,160,150,25);
   contentPane.add(Pressbutton);
  
  
  
   setVisible(true);


}
public void actionPerformed(ActionEvent event) {
     System.out.println("test="+Integer.parseInt(txtdate.getText()));
     print_day(Integer.parseInt(txtdate.getText()), Integer.parseInt(txtmonth.getText()), Integer.parseInt(txtyear.getText()));

     txtresult.updateUI();

}

public void print_day(int d, int m, int y){
       int a =0;
       a = ((y- 1900)*365) + ((y-1900)/4);
       if(y%4 == 0 && (m == 2 || m == 1)){
       a = a-1;
       }
       
       switch(m){
       case 1:
       a = a + d;
       break;
       case 2:
       a = a + 31 + d;
       break;
       case 3:
       a =  a + 31 + 28 +d;
       break;
       case 4:
       a =  a + 31 + 28 + 31 + d;
       break;
       case 5:
       a =  a + 31 + 28 + 31 + 30 + d;
       break;
       case 6:
       a =  a + 31 + 28 + 31 + 30 + 31 + d;
       break;
       case 7:
       a =  a + 31 + 28 + 31 + 30 + 31 + 30 + d;
       break;
       case 8:
       a =  a + 31 + 28 +31 + 30 + 31 + 30 + 31 + d;
       break;
       case 9:
       a =  a + 31 + 28 +31 + 30 + 31 + 30 + 31 + 31 + d;
       break;
       case 10:
       a =  a + 31 + 28 +31 + 30 + 31 + 30 + 31 + 31 + 30 + d;
       break;
       case 11:
       a =  a + 31 + 28 +31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + d;
       break;
       case 12:
       a =  a + 31 + 28 +31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + d;
       break;
       default:
       break;
    }
   
        System.out.println("\n"+ a);
       int odd_days = a % 7;
       switch(odd_days){
       case 0:
           txtresult.setText("Sunday");
       System.out.println("The given date is Sunday");
       break;
       case 1:
           txtresult.setText("Monday");
       System.out.println("The given date is Monday");
       break;
       case 2:
           txtresult.setText("Tuesesday");
       System.out.println("The given date is Tuesday");
       break;
       case 3:
           txtresult.setText("Wednesday");
       System.out.println("The given date is Wednesday");
       break;
       case 4:
           txtresult.setText("Thursday");
       System.out.println("The given date is Thursday");
       break;
       case 5:
           txtresult.setText("Friday");
       System.out.println("The given date is Friday");
       break;
       case 6:
           txtresult.setText("Saturday");
       System.out.println("The given date is Saturday");
       break;
       default:
       break;
    }
}

}
public class Hill {
   
   
public static void main(String[] args){

MyJFrame f= new MyJFrame();


}

沒有留言:

張貼留言

 Python install twine on Mac 1. Need to install macports first, https://ports.macports.org/port/twine/  2.  sudo port install twine   Pychar...