一个日期算法问题
以今天所在的星期为起点,在界面上显示N天之后的日期,补全今天所在星期的日期和N天后所在星期的日期.
如今天是17号,显示10天后的日期(10天后是27后,但需要补全17号所在的星期和27号所在的星期)
显示结果
日 一 二 三 四 五 六
7/15 7/16 7/17 7/18 7/19 7/20 7/21
7/22 7/23 7/24 7/25 7/26 7/27 7/28
谢谢!
------解决方案--------------------/**
* Calendar test
* @author
*
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LeapYearTest extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
Label result;
TextArea txa;
TextField t1;
Button btn;
int year;
boolean leap;
int weekDay;
public LeapYearTest() {
super( "Calendar Test ");
setSize(280, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(dimension.width / 2 - 140, dimension.height / 2 - 350);
JPanel panel = new JPanel();
result = new Label( "year ");
t1 = new TextField(5);
t1.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
char input = e.getKeyChar();
if (!Character.isDigit(input) && input != '\b ') {
e.consume();
}
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
});
btn = new Button( "judge ");
panel.add(result);
panel.add(t1);
panel.add(btn);
txa = new TextArea( " ", 12, 36, TextArea.SCROLLBARS_VERTICAL_ONLY);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(panel, "North ");
this.getContentPane().add(txa, "Center ");
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
year = Integer.parseInt(t1.getText());
t1.setText( " ");
if (year < 1) {
txa.setText( "year canot be less than 1! ");
return;
}
weekDay = (int) firstDayOfYear(year);
txa.append( " Calendar " + year + "\n ");
for (int i = 1; i <= 12; i++) {
printMonth(i);
//System.out.println( "\n ");
txa.append( "\n ");
}
}
private void printMonth(int m) {
printHead(m);
int days = daysOfMonth(m);
for (int i = 1; i <= days; i++) {
if (i < 10) {
//System.out.print( " " + i);
txa.append( " " + i);
} else {
//System.out.print( " " + i);
txa.append( " " + i);
}
weekDay = (weekDay + 1) % 7;
if (weekDay == 0) {
//System.out.println();
txa.append( "\n ");
//System.out.print( " ");// 控制2-6行
txa.append( " ");
}
}
}
private void printHead(int m) {
//System.out.println( "======================================== ");
//System.out.println( " *日 一 二 三 四 五 六* " + m + "月 ");
//System.out.println( "======================================== ");
txa.append( "========================================\n ");
txa.append( " *日 一 二 三 四 五 六* | " + m + "月\n ");
txa.append( "========================================\n ");