//应我们一个无理取闹的java老师,气死我了,教课1+1的水平,作业确是 拉格朗日
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class dataseach extends JFrame implements ActionListener{
static JLabel label1=new JLabel("开学:");
static JLabel label2=new JLabel("当前日期:");
static JTextField text1=new JTextField();
static JTextField text2=new JTextField();
static JButton sbt=new JButton("提交");
static JButton rbt=new JButton("重置");
static JTextArea textA=new JTextArea("输入输出格式为20180101\n");
//main
public static void main(String[] args) {
dataseach ds=new dataseach();
}
//窗口
public dataseach() {
JFrame frm=new JFrame();
frm.setTitle("日期差值");
frm.setLayout(null);
//标签
label1.setHorizontalAlignment(0);
label2.setHorizontalAlignment(0);
label1.setBounds(5, 5, 80, 40);
frm.getContentPane().add(label1);
label2.setBounds(5, 30, 80, 40);
frm.getContentPane().add(label2);
//文本款
text1.setHorizontalAlignment(0);
text1.setBounds(90, 5, 200, 30);
text2.setHorizontalAlignment(0);
text2.setBounds(90, 35, 200, 30);
frm.getContentPane().add(text1);
frm.getContentPane().add(text2);
//按钮
sbt.setBounds(220,80,60,40);
rbt.setBounds(150,80,60,40);
frm.getContentPane().add(sbt);
frm.getContentPane().add(rbt);
//文本域
textA.setLineWrap(false);
textA.setColumns(10);
textA.setRows(10);
textA.setBounds(5, 150, 300, 300);
textA.setEditable(false);
frm.getContentPane().add(textA);
//监听
rbt.addActionListener(this);
sbt.addActionListener(this);
frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
frm.setBounds(400,200,400,300);
frm.setVisible(true);
}
//事件处理
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==sbt)
{
int day=0,week=0;
int date1=0,date2=0;
String d1=text1.getText();
String d2=text2.getText();
date1=Integer.parseInt(d1);
date2=Integer.parseInt(d2);
dataseach dc = new dataseach();
day=dc.deel(date1,date2);
if(day%7>0)
week=day/7+1;
else
week=day/7;
textA.append("指定日期是本学期的第"+week+"周和第"+day+"天\n");
}
else
{
textA.setText("输入输出格式为20180101\n");
text1.setText("");
text2.setText("");
}
}
//日期差值
public int deel(int enter1,int enter2){
int year1, month1, day1, year2, month2, day2;
int i, j,day;
int[][] a= { { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };
day1 = enter1 % 100;
month1 = enter1 % 10000 / 100;
year1 = enter1 / 10000;
day2 = enter2 % 100;
month2 = enter2 % 10000 / 100;
year2 = enter2 / 10000;
if (enter1 < enter2)
{
if ((year1 % 400 == 0) || ((year1 % 4 == 0) && (year1 % 100 != 0)))
i = 1;
else
i = 0;
for (j = 0; j < month1 - 1; j++)
{
day1 += a[i][j];
}
if ((year2 % 400 == 0) || ((year2 % 4 == 0) && (year2 % 100 != 0)))
i = 1;
else
i = 0;
for (j = 0; j < month2 - 1; j++)
{
day2 += a[i][j];
}
day = day2 - day1 + 1;
for (i = year1; i < year2; i++)
{
if ((i% 400 == 0) || ((i % 4 == 0) && (i % 100 != 0)))
day += 366;
else
day += 365;
}
}
else
{
if ((year1 % 400 == 0) || ((year1 % 4 == 0) && (year1 % 100 != 0)))
i = 1;
else
i = 0;
for (j = 0; j < month1 - 1; j++)
{
day1 += a[i][j];
}
if ((year2 % 400 == 0) || ((year2 % 4 == 0) && (year2 % 100 != 0)))
i = 1;
else
i = 0;
for (j = 0; j < month2 - 1; j++)
{
day2 += a[i][j];
}
day = day1 - day2 + 1;
for (i = year2; i < year1; i++)
{
if ((i % 400 == 0) || ((i % 4 == 0) && (i % 100 != 0)))
day += 366;
else
day += 365;
}
}
return day;
}
}