星期的java语言_如何用java语言 写出一个万年历呢? 要求自己输入年份 自动出现月 日 以及对应的星期...

展开全部

如果要月历,只要把月份循环那里修改下,直接调用月历方法既可

import java.text.DateFormatSymbols;

import java.util.Calendar;

import javax.swing.JOptionPane;

public class YearCalendar {

public static void main(String[] args) {

final String title = getCalTitle();

String input = JOptionPane.showInputDialog("Please input year");

try{

if(!32313133353236313431303231363533e4b893e5b19e31333264633466input.trim().matches("^\\d{4}$")){

throw new NumberFormatException();

}

int year = Integer.parseInt(input.trim());

System.out.println("------- Calendar For Year " + year + " ----------------");

String[] monthTitles = new DateFormatSymbols().getMonths();

for(int month = Calendar.JANUARY; month <= Calendar.DECEMBER; month++){

System.out.println("\t********** " + monthTitles[month] + " *********");

System.out.println(title);

generateMonthlyCalendar(year, month);

System.out.println("\n\n");

}

}catch(NumberFormatException nbFmtExp){

JOptionPane.showMessageDialog(null, "Error data foramt! Date should be 4 digits only format yyyy");

System.exit(0);

}

}

private static String getCalTitle() {

StringBuffer sb = new StringBuffer();

String[] ary = new DateFormatSymbols().getShortWeekdays();

for(int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; i++){

sb.append(ary[i]+ "\t");

}

return sb.toString();

}

private static void generateMonthlyCalendar(int year, int month) {

Calendar cal = Calendar.getInstance();

cal.set(Calendar.YEAR, year);

cal.set(Calendar.MONTH, month);

cal.set(Calendar.DATE, 1);

int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

int i = 0;

for(i = Calendar.SUNDAY; i < cal.get(Calendar.DAY_OF_WEEK); i++){

System.out.print(" \t");

}

while(cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY){

System.out.print(cal.get(Calendar.DATE) + "\t");

cal.add(Calendar.DATE, 1);

}

int weekDay = Calendar.SATURDAY;

int day = cal.get(Calendar.DATE);

while(day <= maxDay){

if(weekDay == Calendar.SATURDAY){

System.out.println();

weekDay = Calendar.SUNDAY;

}else{

weekDay++;

}

System.out.print(day++ + "\t");

}

}

}

--------------------------------JDK 1.5结果

------- Calendar For Year 2011 ----------------

********** January *********

Sun Mon Tue Wed Thu Fri Sat

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

********** February *********

Sun Mon Tue Wed Thu Fri Sat

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

********** March *********

Sun Mon Tue Wed Thu Fri Sat

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

********** April *********

Sun Mon Tue Wed Thu Fri Sat

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

********** May *********

Sun Mon Tue Wed Thu Fri Sat

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

********** June *********

Sun Mon Tue Wed Thu Fri Sat

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

********** July *********

Sun Mon Tue Wed Thu Fri Sat

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

********** August *********

Sun Mon Tue Wed Thu Fri Sat

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

********** September *********

Sun Mon Tue Wed Thu Fri Sat

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

********** October *********

Sun Mon Tue Wed Thu Fri Sat

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

********** November *********

Sun Mon Tue Wed Thu Fri Sat

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

********** December *********

Sun Mon Tue Wed Thu Fri Sat

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

追问

布局真乱

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值