这个是别人的代码 我也要做日期加减 看别人的还有些不通 谢谢了import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;import javax.swing.*;public class Main { public static void main(String[] args) {
new MainFrame();
}
}class MainFrame extends JFrame implements ActionListener {
JButton btn1 = new JButton("button1");
JButton btn2 = new JButton("button2"); public MainFrame() {
this.setTitle("Main Frame");
this.setSize(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = this.getContentPane();
c.setLayout(null);
btn1.setBounds(20, 20, 80, 20);
btn2.setBounds(20, 50, 80, 20);
c.add(btn1);
c.add(btn2); btn1.addActionListener(this);
btn2.addActionListener(this); int x = (int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth() - this.getWidth())/2);
int y = (int)((Toolkit.getDefaultToolkit().getScreenSize().getHeight() - this.getHeight())/2);
this.setLocation(x, y); this.setVisible(true);
} public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
Date date = new Date();
if (source == btn1) {
new SubDialog(date, 0);
} else if (source == btn2) {
new SubDialog(date, 1);
}
}
}class SubDialog extends JDialog implements ActionListener {
JButton btn1 = new JButton("calculate");
JButton btn2 = new JButton("close");
JTextField text = new JTextField();
JLabel label0 = new JLabel();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
Date sysDate;
int dialogType = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); public SubDialog(Date date, int dialogType) {
this.setTitle("Sub Dialog");
this.setSize(360, 200);
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); Container c = this.getContentPane();
c.setLayout(null);
label0.setBounds(20, 20, 200, 20);
text.setBounds(20, 50, 80, 20);
label1.setBounds(110, 50, 240, 20);
label2.setBounds(20, 80, 160, 20);
btn1.setBounds(20, 110, 80, 20);
btn2.setBounds(110, 110, 80, 20);
c.add(label0);
c.add(text);
c.add(label1);
c.add(label2);
c.add(btn1);
c.add(btn2); sysDate = date;
label0.setText("System Date is " + sdf.format(date));
this.dialogType = dialogType;
if (dialogType == 1) {
label1.setText("(Please inpupt a [yyyy-MM-dd] string)");
} else {
label1.setText("(Please input a Number)");
} btn1.addActionListener(this);
btn2.addActionListener(this); int x = (int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth() - this.getWidth())/2);
int y = (int)((Toolkit.getDefaultToolkit().getScreenSize().getHeight() - this.getHeight())/2);
this.setLocation(x, y); this.setModal(true);
this.setVisible(true); } public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == btn1) {
if (dialogType == 1) {
calculateCount();
} else {
calculateDate();
}
} else if (source == btn2) {
this.dispose();
}
} private void calculateCount() {
try {
String s = text.getText();
Date date = sdf.parse(s);
int count = 0;
Calendar c1 = Calendar.getInstance(); 这是什么意思 新定义的有什么特殊吗
c1.setTime(sysDate);
Calendar c2 = Calendar.getInstance();
c2.setTime(date); if (date.before(sysDate)) { 这为什么要对于时间进行比较难道二者不都是系统时间马
while (c2.after(c1) == false) { 那都分别代表什么时间呢
count++;
c2.add(Calendar.DATE, 1);
}
count *= -1; 这就是在计算了吧 不能常规的加减马 这是倒数呢???
} else {
while (c2.before(c1) == false) {
count++;
c2.add(Calendar.DATE, -1);
}
} label2.setText("The date count is [" + count + "]"); } catch (Throwable e) {
label2.setText("input date error.");
}
} private void calculateDate() {
try {
String s = text.getText();
int count = Integer.valueOf(s).intValue();
Calendar c1 = Calendar.getInstance();
c1.setTime(sysDate);
c1.add(Calendar.DATE, count);
label2.setText("The date is [" + sdf.format(c1.getTime()) + "]"); } catch (Throwable e) {
label2.setText("input date error.");
}
}
}
import java.awt.event.*;
import java.text.*;
import java.util.*;import javax.swing.*;public class Main { public static void main(String[] args) {
new MainFrame();
}
}class MainFrame extends JFrame implements ActionListener {
JButton btn1 = new JButton("button1");
JButton btn2 = new JButton("button2"); public MainFrame() {
this.setTitle("Main Frame");
this.setSize(200, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = this.getContentPane();
c.setLayout(null);
btn1.setBounds(20, 20, 80, 20);
btn2.setBounds(20, 50, 80, 20);
c.add(btn1);
c.add(btn2); btn1.addActionListener(this);
btn2.addActionListener(this); int x = (int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth() - this.getWidth())/2);
int y = (int)((Toolkit.getDefaultToolkit().getScreenSize().getHeight() - this.getHeight())/2);
this.setLocation(x, y); this.setVisible(true);
} public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
Date date = new Date();
if (source == btn1) {
new SubDialog(date, 0);
} else if (source == btn2) {
new SubDialog(date, 1);
}
}
}class SubDialog extends JDialog implements ActionListener {
JButton btn1 = new JButton("calculate");
JButton btn2 = new JButton("close");
JTextField text = new JTextField();
JLabel label0 = new JLabel();
JLabel label1 = new JLabel();
JLabel label2 = new JLabel();
Date sysDate;
int dialogType = 0;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); public SubDialog(Date date, int dialogType) {
this.setTitle("Sub Dialog");
this.setSize(360, 200);
this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); Container c = this.getContentPane();
c.setLayout(null);
label0.setBounds(20, 20, 200, 20);
text.setBounds(20, 50, 80, 20);
label1.setBounds(110, 50, 240, 20);
label2.setBounds(20, 80, 160, 20);
btn1.setBounds(20, 110, 80, 20);
btn2.setBounds(110, 110, 80, 20);
c.add(label0);
c.add(text);
c.add(label1);
c.add(label2);
c.add(btn1);
c.add(btn2); sysDate = date;
label0.setText("System Date is " + sdf.format(date));
this.dialogType = dialogType;
if (dialogType == 1) {
label1.setText("(Please inpupt a [yyyy-MM-dd] string)");
} else {
label1.setText("(Please input a Number)");
} btn1.addActionListener(this);
btn2.addActionListener(this); int x = (int)((Toolkit.getDefaultToolkit().getScreenSize().getWidth() - this.getWidth())/2);
int y = (int)((Toolkit.getDefaultToolkit().getScreenSize().getHeight() - this.getHeight())/2);
this.setLocation(x, y); this.setModal(true);
this.setVisible(true); } public void actionPerformed(ActionEvent event) {
Object source = event.getSource();
if (source == btn1) {
if (dialogType == 1) {
calculateCount();
} else {
calculateDate();
}
} else if (source == btn2) {
this.dispose();
}
} private void calculateCount() {
try {
String s = text.getText();
Date date = sdf.parse(s);
int count = 0;
Calendar c1 = Calendar.getInstance(); 这是什么意思 新定义的有什么特殊吗
c1.setTime(sysDate);
Calendar c2 = Calendar.getInstance();
c2.setTime(date); if (date.before(sysDate)) { 这为什么要对于时间进行比较难道二者不都是系统时间马
while (c2.after(c1) == false) { 那都分别代表什么时间呢
count++;
c2.add(Calendar.DATE, 1);
}
count *= -1; 这就是在计算了吧 不能常规的加减马 这是倒数呢???
} else {
while (c2.before(c1) == false) {
count++;
c2.add(Calendar.DATE, -1);
}
} label2.setText("The date count is [" + count + "]"); } catch (Throwable e) {
label2.setText("input date error.");
}
} private void calculateDate() {
try {
String s = text.getText();
int count = Integer.valueOf(s).intValue();
Calendar c1 = Calendar.getInstance();
c1.setTime(sysDate);
c1.add(Calendar.DATE, count);
label2.setText("The date is [" + sdf.format(c1.getTime()) + "]"); } catch (Throwable e) {
label2.setText("input date error.");
}
}
}
下一个问题,只要知道上面的就解决了,只不过用了一个时间格式器来生成时间对象Date date = sdf.parse(s);
count = count*-1;