如何用java语言 写出一个万年历呢? 要求自己输入年份 自动出现月 日 以及对应的星期
4个回答
展开全部
如果要月历,只要把月份循环那里修改下,直接调用月历方法既可
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(!input.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
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(!input.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
追问
布局真乱
展开全部
import java.util.*;
public class B{
public static void main(String []args){
System.out.println("Please input the year");
Scanner scanner =new Scanner(System.in);
int year=0;
int day=0;
int week=0;
try{
year=scanner.nextInt();
System.out.println("the calendar by"+year+"year");
}
catch(Exception ex){
System.out.println("Please enter an integer");
}
Calendar now = Calendar.getInstance();
for(int i=1;i<=12;i++){
switch(i){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day= 31;
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
case 2:
if ((year % 4 == 0 && year% 100 != 0) || year % 400 == 0) {
day = 29;
} else {
day = 28;
}
}
now.set(year,i-1,1);
week = now.get(Calendar.DAY_OF_WEEK);
boolean toweek=false;
System.out.println(year+"年"+i+"月");
System.out.println(" 日 "+" 一 "+" 二 "+" 三 "+" 四 "+" 五 "+" 六 ");
for(int num=1;num<=day;){
for(int j=1;j<=7;){
if(!toweek){
if(j!=week){
System.out.print(" ");
j++;
}else{
toweek=true;
}
}
if(toweek){
if(num<10){
System.out.print(" "+num+" ");
}
else{System.out.print(" "+num+" ");}
j++;
num++;
if(num>day){
break;
}
}
}
System.out.println();
}
System.out.println();
System.out.println();
}
}
}
类名B
public class B{
public static void main(String []args){
System.out.println("Please input the year");
Scanner scanner =new Scanner(System.in);
int year=0;
int day=0;
int week=0;
try{
year=scanner.nextInt();
System.out.println("the calendar by"+year+"year");
}
catch(Exception ex){
System.out.println("Please enter an integer");
}
Calendar now = Calendar.getInstance();
for(int i=1;i<=12;i++){
switch(i){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day= 31;
break;
case 4:
case 6:
case 9:
case 11:
day = 30;
break;
case 2:
if ((year % 4 == 0 && year% 100 != 0) || year % 400 == 0) {
day = 29;
} else {
day = 28;
}
}
now.set(year,i-1,1);
week = now.get(Calendar.DAY_OF_WEEK);
boolean toweek=false;
System.out.println(year+"年"+i+"月");
System.out.println(" 日 "+" 一 "+" 二 "+" 三 "+" 四 "+" 五 "+" 六 ");
for(int num=1;num<=day;){
for(int j=1;j<=7;){
if(!toweek){
if(j!=week){
System.out.print(" ");
j++;
}else{
toweek=true;
}
}
if(toweek){
if(num<10){
System.out.print(" "+num+" ");
}
else{System.out.print(" "+num+" ");}
j++;
num++;
if(num>day){
break;
}
}
}
System.out.println();
}
System.out.println();
System.out.println();
}
}
}
类名B
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Scanner;
public class Calender {
public static boolean isLeapYear(int year){
if(year<=0){
System.out.println("输入数据有误!请输入正整数。");
return false;
}else if(year%4==0&&year%100!=0||year%400==0){
return true;
}else{
return false;
}
}
public static int getWeek(int year){
int week = (year+(year-1)/4+(year-1)/400-(year-1)/100)%7;
return week;
}
public static void main(String[] args) {
int year;
Scanner in=new Scanner(System.in);
System.out.println("请输入年份:");
year=in.nextInt();
int normalYear[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int leapYear[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
System.out.println("*******************公元"+year+"日历*******************");
System.out.println();
boolean isLeap = isLeapYear(year);
int week = getWeek(year);
if(week==0){
week=7;
}
for(int i = 1;i<=12;i++){
System.out.println("************************"+i+"月***********************");
System.out.println("一\t二\t三\t四\t五\t六\t日");
int tab = week-1;
if(isLeap){
for(int j=0;j<i;j++){
tab=tab+leapYear[j];
}
tab=tab%7;
for(int k=1;k<=tab;k++){
System.out.print("\t");
}
for(int j=1;j<=leapYear[i];j++){
if((tab+j)%7==0){
System.out.println(j);
}else if(j<10){
System.out.print(""+j+"\t");
}else{
System.out.print(j+"\t");
}
}
System.out.println();
System.out.println();
}else{
for(int j=0;j<i;j++){
tab=tab+normalYear[j];
}
tab=tab%7;
for(int k=1;k<=tab;k++){
System.out.print("\t");
}
for(int j=1;j<=normalYear[i];j++){
if((tab+j)%7==0){
System.out.println(j);
}else if(j<10){
System.out.print(""+j+"\t");
}else{
System.out.print(j+"\t");
}
}
System.out.println();
System.out.println();
}
}
}
}
public class Calender {
public static boolean isLeapYear(int year){
if(year<=0){
System.out.println("输入数据有误!请输入正整数。");
return false;
}else if(year%4==0&&year%100!=0||year%400==0){
return true;
}else{
return false;
}
}
public static int getWeek(int year){
int week = (year+(year-1)/4+(year-1)/400-(year-1)/100)%7;
return week;
}
public static void main(String[] args) {
int year;
Scanner in=new Scanner(System.in);
System.out.println("请输入年份:");
year=in.nextInt();
int normalYear[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int leapYear[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
System.out.println("*******************公元"+year+"日历*******************");
System.out.println();
boolean isLeap = isLeapYear(year);
int week = getWeek(year);
if(week==0){
week=7;
}
for(int i = 1;i<=12;i++){
System.out.println("************************"+i+"月***********************");
System.out.println("一\t二\t三\t四\t五\t六\t日");
int tab = week-1;
if(isLeap){
for(int j=0;j<i;j++){
tab=tab+leapYear[j];
}
tab=tab%7;
for(int k=1;k<=tab;k++){
System.out.print("\t");
}
for(int j=1;j<=leapYear[i];j++){
if((tab+j)%7==0){
System.out.println(j);
}else if(j<10){
System.out.print(""+j+"\t");
}else{
System.out.print(j+"\t");
}
}
System.out.println();
System.out.println();
}else{
for(int j=0;j<i;j++){
tab=tab+normalYear[j];
}
tab=tab%7;
for(int k=1;k<=tab;k++){
System.out.print("\t");
}
for(int j=1;j<=normalYear[i];j++){
if((tab+j)%7==0){
System.out.println(j);
}else if(j<10){
System.out.print(""+j+"\t");
}else{
System.out.print(j+"\t");
}
}
System.out.println();
System.out.println();
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
发生法
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询