java万年历代码视频 java万年历课程设计( 六 )


}
public void setDate(Date date) {
jFormattedTextField.setText(getDefaultDateFormat().format(date));
}
public Date getDate() {
try {
String dateString = jFormattedTextField.getText();
return getDefaultDateFormat().parse(dateString);
} catch (ParseException e) {
return getNowDate();
} catch (Exception ee) {
return getNowDate();
}
}
private static Date getNowDate() {
return Calendar.getInstance().getTime();
}
private static SimpleDateFormat getDefaultDateFormat() {
return new SimpleDateFormat("yyyy-MM-ddHH:mm");
}
}
测试程序:
package Demo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class DemoForDateChooser extends JFrame {
private JFormattedTextField formattedTextField;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
DemoForDateChooser frame = new DemoForDateChooser();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
public DemoForDateChooser() {
super();
setTitle("日期选择框");
getContentPane().setLayout(null);
setBounds(100, 100, 383, 137);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel panel = new JPanel();
panel.setLayout(null);
panel.setBounds(0, 0, 375, 107);
getContentPane().add(panel);
formattedTextField = new JFormattedTextField();
formattedTextField.setBounds(68, 48, 175, 23);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
DateChooser mDateChooser=new DateChooser(formattedTextField);
Point p = button.getLocationOnScreen();
p.y = p.y + 30;
mDateChooser.showDateChooser(p);
formattedTextField.requestFocusInWindow();
}
});
button.setText("选择日期");
button.setBounds(249, 47, 99, 23);
panel.add(button);
final JLabel label = new JLabel();
label.setText("日期:");
label.setBounds(10, 47, 71, 23);
panel.add(label);
panel.add(formattedTextField);
//
}
}
java中,一段万年历代码,跑起来总是差4天,修改之后也只能让一部分月份的显示正确,代码如下我改造了一下你的代码 :
package com.mikuma.calendar;
import java.util.GregorianCalendar;
import java.util.Scanner;
public class Calendar{
public static void main(String[] args){
int year = 0;
int month = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您要查询的年份");
year = scanner.nextInt();
System.out.println("请输入您要查询的月份");
while (true){
month = scanner.nextInt();
if (month0 || month12){
System.out.println("月份输入有误 , 请重新输入");
}else{
break;
}
}
printPermanentCalendar(year, month);
}
/**
* 输出万年历
*
* @param year
* @param month
*/
private static void printPermanentCalendar(int year,int month){
int days = 0;
int totaldays = 0;//获取1990年至查询的年份的天数
for (int i = 1900; iyear; i++){
totaldays = totaldays + (isLeapYear(i) ? 366 : 365);
}
int beforedays = 0;//到指定月份的天数
for (int i = 1; i = month; i++){
switch (i) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:

推荐阅读