一,Java實現萬年歷的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
package calendar; import java.util.Scanner; //日歷項目 public class RiLi{ public static void main(String[] args){ Scanner A= new Scanner(System.in); System.out.println( "請輸入年份:" ); int year=A.nextInt(); System.out.println( "請輸入月份:" ); int month=A.nextInt(); int sum= 0 ; for ( int i= 1900 ;i<year;i++){ if (i% 4 == 0 &&i% 100 != 0 ||i% 400 == 0 ){ sum=sum+ 366 ; } else { sum=sum+ 365 ; } } for ( int i= 1 ;i<month;i++){ if (i== 2 ){ if (year% 4 == 0 &&year% 100 != 0 ||year% 400 == 0 ){ sum=sum+ 29 ;} else { sum=sum+ 28 ; } } else { if (i== 4 ||i== 6 ||i== 9 ||i== 11 ){ sum+= 30 ; } else { sum+= 31 ; } } } sum=sum+ 1 ; int wekday=sum% 7 ; System.out.println( "日\t一\t二\t三\t四\t五\t六" ); for ( int i= 1 ;i<=wekday;i++){ System.out.print( "\t" ); } int f= 0 ; if (month== 4 ||month== 6 ||month== 9 ||month== 11 ){ f= 30 ;} if (month== 1 ||month== 3 ||month== 5 ||month== 7 ||month== 8 ||month== 10 ||month== 12 ){f= 31 ;} if (month== 2 ){ if (year% 4 == 0 &&year% 100 != 0 ||year% 400 == 0 ){f= 29 ;} else {f= 28 ;} } for ( int i= 1 ;i<=f;i++){ if (sum% 7 == 6 ){ System.out.print(i+ "\n" ); } else { System.out.print(i+ "\t" ); } sum++; } } } <br> |
二.一個Java萬年歷,比較簡單的那種,顯示年月日、星期幾、當前日期是第幾周、顯示閏年、打印萬年歷等,還可顯示當前日期是一年中的第幾天,指定日期是星期幾等,采用了基姆拉爾森計算公式 ,W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7 ,在公式中d表示日期中的日數,m表示月份數,y表示年數。注意:在公式中有個與其他公式不同的地方: 把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10則換算成:2003-13-10來代入公式計算。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
public class myCalendar { //以下程序段是用來計算輸入日期為該年的第幾天的 public static int cptDay( int year , int month , int day){ byte dayadd[]={ 1 ,- 2 , 1 , 0 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 1 }; //用來存儲每個月天數和30的差值 int daycount = 0 ; //這是天數daycount計數器,初始化為0 for ( int i= 0 ; i<month- 1 ; i++) daycount+=( 30 +dayadd[i]); daycount+=day; return (month> 2 )?daycount+isLeap(year):daycount; } //閏年判定程序段,閏年返回1,平年返回0 public static int isLeap( int year){ if ((year% 400 == 0 )||((year% 4 == 0 )&&(year% 100 != 0 ))) return 1 ; return 0 ; } //計算輸入日期是星期幾 //采用了基姆拉爾森計算公式 //W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7 //在公式中d表示日期中的日數,m表示月份數,y表示年數。 //注意:在公式中有個與其他公式不同的地方: //把一月和二月看成是上一年的十三月和十四月,例:如果是2004-1-10則換算成:2003-13-10來代入公式計算。 public static int getWeek( int year, int month, int day){ if (month< 3 ) { month+= 12 ; year--;} return (day+ 2 *month+ 3 *(month+ 1 )/ 5 +year+year/ 4 -year/ 100 +year/ 400 )% 7 ; } //以下程序段是來計算輸入日期為該年第幾周的 public static int weekCount( int year, int month, int day){ int dayCnt = cptDay(year,month,day); int weekminus = getWeek(year,month,day)-getWeek(year, 1 , 1 ); int weekCnt = 0 ; if (dayCnt% 7 == 0 ) weekCnt = dayCnt/ 7 +((weekminus> 0 )? 1 : 0 ); else weekCnt = dayCnt/ 7 +((weekminus> 0 )? 2 : 1 ); return weekCnt; } //打印萬年歷 public static void printCal( int year){ byte dayadd[]={ 0 , 1 ,- 2 , 1 , 0 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 1 }; //同樣的,每月天數和30的差值,注意,dadadd[0]的0并沒用到 int wkpoint = getWeek(year, 1 , 1 ); //wkpoint用來指出當前日期的星期數 int t = 0 ; //t用來作為一個標記器,解決閏年2月有29天的問題 int bk = 0 ; //bk用來記錄需輸空白的數目 String week[]={ "星期一" , "星期二" , "星期三" , "星期四" , "星期五" , "星期六" , "星期日" }; for ( int i= 1 ;i< 13 ;i++) { t = 0 ; bk = 0 ; if ((i== 2 )&&(isLeap(year)== 1 )) t = 1 ; //當且僅當閏年的2月份才將其置為1 System.out.println( "\n\n\t\t" +year+ " 年 " +i+ " 月\n" ); for ( int j= 0 ;j< 7 ;j++) System.out.print(week[j]+ "\t" ); System.out.println(); while (bk++<wkpoint) System.out.print( '\t' ); for ( int j= 1 ;j<=( 30 +dayadd[i]+t);j++) { System.out.print(j+ "\t" ); //循環輸出每月日期 if (wkpoint== 6 ) { wkpoint = 0 ; System.out.print( '\n' );} //當wkpoint計數器為6時將其置為0,并換行 else wkpoint++; } } } public static void main(String[] args){ String week[]={ "星期一" , "星期二" , "星期三" , "星期四" , "星期五" , "星期六" , "星期日" }; System.out.println( "輸入的日期是該年的第" +cptDay( 2009 , 2 , 15 )+ "天" ); System.out.println( "這一天是該年的第" +weekCount( 2009 , 2 , 15 )+ "周 " +week[getWeek( 2009 , 2 , 15 )]); printCal( 2009 ); } } |
三、
1.用戶輸入信息-->進行信息判斷(是否符合要求)
2.以1900年1月1日(星期一)為基準,計算1900年1月1日至當日的總天數
(1)先計算1900年至(用戶輸入的年 - 1)的總天數 -->注意平閏年之分
(2)計算用戶輸入年份的1月至(用戶輸入月份 - 1)的天數
3.計算輸入月份的第一天是星期幾
4.格式化輸出
以下我們按照步驟一步一步進行代碼解析
一)、利用do-while循環接受用戶輸入信息,并利用if-else語句進行判斷
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
int year; int month; boolean xn = false ; do { System.out.println( "請輸入年份:" ); year = input.nextInt(); System.out.println( "請輸入月份:" ); month = input.nextInt(); //利用boolean類型的表達式進行輸入信息的判斷 xn = (month < 1 ) || (month > 12 ) ||(year < 1 ); if (xn) { System.out.println( "輸入信息錯誤,請重新輸入!" ); } } while (xn); |
二)、判斷平閏年,并計算1900年至(用戶輸入年 - 1)的總天數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
int everyYearDay = 0 ; //每年的天數 int totalYearsDays = 0 ; //計算年的天數 int inputYearDay = 0 //記錄用戶輸入年的天數 boolean yn = false ; //標識平閏年 //利用for循環計算天數 for ( int i = 1900 ;i < =year;i ++) { if (((i % 4 == 0 )&&(i % 100 != 0 ))||(i % 400 == 0 )) //閏年的判斷條件 { yn = true ; everyYearDay = 366 ; } else { yn = false ; everyYearDay = 365 ; } //如果循環中的年份小于用戶輸入的年份,則累積天數 if (i < year) { totalYearsDays = totalYearsDays + everyYearDay; } else { inputYearDay = everyYearDay; System.out.println(year + "年共有:" + inputYearDay + "天" ); } } |
三)、判斷月份的天數,并計算當年1月至(用戶輸入月份 -1)的天數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
int everyMonthDay = 0 ; //記錄每月的天數 int totalMonthsDays = 0 ; //總天數 int inputMonthDay = 0 ; //記錄用戶輸入的月份在用戶輸入年份的天數 //利用for循環計算天數 for ( int i = 1 ;i <= month;i ++) { switch (i) { case 4 : case 6 : case 9 : case 11 : everyMonthDay = 30 ; break ; case 2 : if (xn) //xn是用來記錄平閏年的boolean類型的變量 { everyMonthDay = 29 ; } else { everyMonthDay = 28 ; } break ; default : everyMonthDay = 31 ; break ; } if (i < month) { totalMonthsDays = totalMonthsDays + everyMonthDay; } else { inputMonthDay = everyMonthDay; System.out.println(month + "月共有:" + inputMonthDay + "天" ); } } |
四)、計算總天數,并計算出用戶輸入的月份的第一天星期幾
int total = totalMonthsDays + totalYearsDays; //計算總天數
int temp = (total + 1) % 7; //判斷輸入月份的第一天星期幾
五)、格式化輸出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
//因為我們的輸入格式是 //星期日 星期一 星期二 星期三 星期四 星期五 星期六 //當星期日時的時候,我們直接輸出就好了,但是當 //第一天是星期一的時候,我們就必須首先打印出空格 //然后再輸出日期,才能讓號碼和星期想對應 //打印空格 for ( int spaceno = 0 ;spaceno < temp;spaceno ++) { System.out.print( "/t" ); } //按照順序打印號碼 for ( int i = 1 ;i <= inputMonthDay;i ++) { if ((total + i ) % 7 ) //判斷是否該換行了 { System.out.println(i ); } else { System.out.print(i + "/t" ); } } |
四、 java(用calendar類)寫一個萬年歷,輸入年并且顯示當年的日歷
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
public class MyCalendar { public static void main(String args[]) { Scanner sc = new Scanner(System.in); Calendar c = Calendar.getInstance(); System.out.println( "請輸入數字年:(比如 2014)" ); int year = sc.nextInt(); sc.close(); // int year = 2014; c.set(Calendar.YEAR,year); for ( int i = 0 ; i < 12 ; i++) { c.set(Calendar.MONTH,i); // c.set(Calendar.DATE, 1 ); //設置成1月 printMonth(c); } } public static void printMonth(Calendar c){ c.set(Calendar.DAY_OF_MONTH, 1 ); //設置成一日 System.out.printf( "\n\n========= %s 月 =========\n" ,c.get(Calendar.MONTH)+ 1 ); String[] weeks = { "日" , "一" , "二" , "三" , "四" , "五" , "六" }; for ( int i = 0 ; i < weeks.length; i++) { System.out.printf( "%s" + (i != 6 ? "\t" : "\n" ),weeks[i]); } int offday = c.get(Calendar.DAY_OF_WEEK) - 1 ; for ( int i = 0 ; i < offday; i++){ System.out.printf( "\t" ); } int month = c.get(Calendar.MONTH); while (c.get(Calendar.MONTH) == month ){ System.out.printf( "%d" + ( (c.get(Calendar.DAY_OF_WEEK)) != 7 ? "\t" : "\n" ) ,c.get(Calendar.DAY_OF_MONTH)); c.add(Calendar.DAY_OF_MONTH, 1 ); } } } |
五、程序:萬年歷+時鐘小程序實現
java知識點有:java常用內庫與工具(Date類、Calendar類等)、異常(try.....catch)、線程、AWT圖形用戶界面等基礎知識點。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.text.*; import java.util.*; //創建窗口和年歷 class MainFrame extends JFrame{ JPanel panel= new JPanel( new BorderLayout()); //BorderLayout為邊界布局 JPanel panel1= new JPanel(); JPanel panel2= new JPanel( new GridLayout( 7 , 7 )); //GridLayout為網格布局 JPanel panel3= new JPanel(); JLabel []label= new JLabel[ 49 ]; JLabel y_label= new JLabel( "年份" ); JLabel m_label= new JLabel( "月份" ); JComboBox com1= new JComboBox(); JComboBox com2= new JComboBox(); JButton button= new JButton( "查看" ); int re_year,re_month; int x_size,y_size; String year_num; Calendar now=Calendar.getInstance(); //實例化Calendar MainFrame(){ super ( "萬年歷-Jackbase" ); setSize( 300 , 350 ); x_size=( int )(Toolkit.getDefaultToolkit().getScreenSize().getWidth()); y_size=( int )(Toolkit.getDefaultToolkit().getScreenSize().getHeight()); setLocation((x_size- 300 )/ 2 ,(y_size- 350 )/ 2 ); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel1.add(y_label); panel1.add(com1); panel1.add(m_label); panel1.add(com2); panel1.add(button); for ( int i= 0 ;i< 49 ;i++){ label= new JLabel( "" ,JLabel.CENTER); //將顯示的字符設置為居中 panel2.add(label); } panel3.add( new Clock( this )); panel.add(panel1,BorderLayout.NORTH); panel.add(panel2,BorderLayout.CENTER); panel.add(panel3,BorderLayout.SOUTH); panel.setBackground(Color.white); panel1.setBackground(Color.white); panel2.setBackground(Color.white); panel3.setBackground(Color.white); Init(); button.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent e){ int c_year,c_month,c_week; c_year=Integer.parseInt(com1.getSelectedItem().toString()); //得到當前所選年份 c_month=Integer.parseInt(com2.getSelectedItem().toString())- 1 ; //得到當前月份,并減1,計算機中的月為0-11 c_week=use(c_year,c_month); //調用函數use,得到星期幾 Resetday(c_week,c_year,c_month); //調用函數Resetday }}); setContentPane(panel); setVisible( true ); setResizable( false ); } public void Init(){ int year,month_num,first_day_num; String log[]={ "日" , "一" , "二" , "三" , "四" , "五" , "六" }; for ( int i= 0 ;i< 7 ;i++){ label.setText(log); } for ( int i= 0 ;i< 49 ;i=i+ 7 ){ label.setForeground(Color.red); //將星期日的日期設置為紅色 } for ( int i= 6 ;i< 49 ;i=i+ 7 ){ label.setForeground(Color.red); //將星期六的日期也設置為紅色 } for ( int i= 1 ;i< 10000 ;i++){ com1.addItem( "" +i); } for ( int i= 1 ;i< 13 ;i++){ com2.addItem( "" +i); } month_num=( int )(now.get(Calendar.MONTH)); //得到當前時間的月份 year=( int )(now.get(Calendar.YEAR)); //得到當前時間的年份 com1.setSelectedIndex(year- 1 ); //設置下拉列表顯示為當前年 com2.setSelectedIndex(month_num); //設置下拉列表顯示為當前月 first_day_num=use(year,month_num); Resetday(first_day_num,year,month_num); } public int use( int reyear, int remonth){ int week_num; now.set(reyear,remonth, 1 ); //設置時間為所要查詢的年月的第一天 week_num= ( int )(now.get(Calendar.DAY_OF_WEEK)); //得到第一天的星期 return week_num; } public void Resetday( int week_log, int year_log, int month_log){ int month_score_log; //判斷是否是閏年的標記 int month_day_score; //存儲月份的天數 int count; month_score_log= 0 ; month_day_score= 0 ; count= 1 ; if (year_log% 4 == 0 &&year_log% 100 != 0 ||year_log% 400 == 0 ){ //判斷是否為閏年 month_score_log= 1 ; } month_log=month_log+ 1 ; //將傳來的月份數加1 switch (month_log){ case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 : month_day_score= 31 ; break ; case 4 : case 6 : case 9 : case 11 : month_day_score= 30 ; break ; case 2 : if (month_score_log== 1 ){ month_day_score= 29 ; } else { month_day_score= 28 ; } break ; } for ( int i= 7 ;i< 49 ;i++){ //初始化標簽 label.setText( "" ); } week_log=week_log+ 6 ; //將星期數加6,使顯示正確 month_day_score=month_day_score+week_log; for ( int i=week_log;i<month_day_score;i++,count++){ label.setText(count+ "" ); } } } //創建時鐘 class Clock extends Canvas implements Runnable{ MainFrame mf; Thread t; String time; Clock(MainFrame mf){ this .mf=mf; setSize( 400 , 40 ); setBackground(Color.white); t= new Thread( this ); //實例化線程 t.start(); //調用線程 } public void run(){ while ( true ){ try { t.sleep( 1000 ); //休眠1秒鐘 } catch (InterruptedException e){ System.out.println( "異常" ); } this .repaint( 100 ); } } public void paint(Graphics g){ Font f= new Font( "宋體" ,Font.BOLD, 16 ); SimpleDateFormat SDF= new SimpleDateFormat( "yyyy'年'MM'月'dd'日'HH:mm:ss" ); //格式化時間顯示類型 Calendar now=Calendar.getInstance(); time=SDF.format(now.getTime()); //得到當前日期和時間 g.setFont(f); g.setColor(Color.red); g.drawString(time, 100 , 25 ); } } public class Wnl{ public static void main(String [] args){ JFrame.setDefaultLookAndFeelDecorated( true ); MainFrame start= new MainFrame(); } } |
以上就是Java 實現萬年歷的資料,希望能幫助實現該功能的朋友,謝謝大家對本站的支持!