package com; import java.util.*; public class Wan { /** * @param args */ public static void main(String[] args) { System.out.println("****************************欢迎使用万年历*****************************" +"\n\n"); Scanner input=new Scanner(System.in); System.out.print("请输入年份:"); int year=input.nextInt(); System.out.print("\n请输入月份:"); int month=input.nextInt(); int day=0;//当前月有几天 switch(month){ 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; break; } int days=0;//在当前年之前的天数 for(int i=1990;i<year;i++){ if((year%4==0&&year%100!=0)||(year%400==0)) days+=366; else days+=365; } for(int i=1;i<month;i++){ switch(i){ case 1: case 3: case 5: case 7: case 8: case 10: case 12: days+=31; break; case 4: case 6: case 9: case 11: days+=30; break; case 2: if((year%4==0&&year%100!=0)||(year%400==0)) days+=29; else days+=28; break; } } System.out.println("\n\n星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六"); int konggeshu=days%7+1;//空格数 if(konggeshu%7==0){ konggeshu=0; } //打印空格 for(int i=1;i<=konggeshu;i++) { System.out.print("\t"); } //打印日期 for(int i=1;i<=day;i++){ System.out.print(i+"\t"); if((konggeshu+i)%7==0) System.out.println(); }
}} 1990年的1月1日星期一
import java.text.DateFormatSymbols; import java.util.*; public class CalendarTest { public static void main(String[] args) { // construct d as current date GregorianCalendar d = new GregorianCalendar(); int today = d.get(Calendar.DAY_OF_MONTH); int month = d.get(Calendar.MONTH); // set d to start date of the month d.set(Calendar.DAY_OF_MONTH, 1); int weekday = d.get(Calendar.DAY_OF_WEEK); // get first day of week (Sunday in the U.S.) int firstDayOfWeek = d.getFirstDayOfWeek(); // determine the required indentation for the first line int indent = 0; while (weekday != firstDayOfWeek) { indent++; d.add(Calendar.DAY_OF_MONTH, -1); weekday = d.get(Calendar.DAY_OF_WEEK); } // print weekday names String[] weekdayNames = new DateFormatSymbols().getShortWeekdays(); do { System.out.printf("%4s", weekdayNames[weekday]); d.add(Calendar.DAY_OF_MONTH, 1); weekday = d.get(Calendar.DAY_OF_WEEK); } while (weekday != firstDayOfWeek); System.out.println(); for (int i = 1; i <= indent; i++) System.out.print(" "); d.set(Calendar.DAY_OF_MONTH, 1); do { // print day int day = d.get(Calendar.DAY_OF_MONTH); System.out.printf("%3d", day); // current day with * if (day == today) System.out.print("*"); else System.out.print(" "); // advance d to the next day d.add(Calendar.DAY_OF_MONTH, 1); weekday = d.get(Calendar.DAY_OF_WEEK); // start a new line at the start of the week if (weekday == firstDayOfWeek) System.out.println(); } while (d.get(Calendar.MONTH) == month); // the loop exits when d is day 1 of the next month // print final end of line if necessary if (weekday != firstDayOfWeek) System.out.println(); } }运行下上面的代码,对你有帮助的!
import java.util.*;
public class Wan { /**
* @param args
*/
public static void main(String[] args) {
System.out.println("****************************欢迎使用万年历*****************************" +"\n\n");
Scanner input=new Scanner(System.in);
System.out.print("请输入年份:");
int year=input.nextInt();
System.out.print("\n请输入月份:");
int month=input.nextInt();
int day=0;//当前月有几天
switch(month){
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;
break;
}
int days=0;//在当前年之前的天数
for(int i=1990;i<year;i++){
if((year%4==0&&year%100!=0)||(year%400==0))
days+=366;
else
days+=365;
}
for(int i=1;i<month;i++){
switch(i){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days+=31;
break;
case 4:
case 6:
case 9:
case 11:
days+=30;
break;
case 2:
if((year%4==0&&year%100!=0)||(year%400==0))
days+=29;
else
days+=28;
break;
}
}
System.out.println("\n\n星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
int konggeshu=days%7+1;//空格数
if(konggeshu%7==0){
konggeshu=0;
}
//打印空格
for(int i=1;i<=konggeshu;i++)
{
System.out.print("\t");
}
//打印日期
for(int i=1;i<=day;i++){
System.out.print(i+"\t");
if((konggeshu+i)%7==0)
System.out.println();
}
}}
1990年的1月1日星期一
import java.text.DateFormatSymbols;
import java.util.*;
public class CalendarTest {
public static void main(String[] args) {
// construct d as current date
GregorianCalendar d = new GregorianCalendar(); int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONTH); // set d to start date of the month
d.set(Calendar.DAY_OF_MONTH, 1); int weekday = d.get(Calendar.DAY_OF_WEEK); // get first day of week (Sunday in the U.S.)
int firstDayOfWeek = d.getFirstDayOfWeek(); // determine the required indentation for the first line
int indent = 0;
while (weekday != firstDayOfWeek) {
indent++;
d.add(Calendar.DAY_OF_MONTH, -1);
weekday = d.get(Calendar.DAY_OF_WEEK);
} // print weekday names
String[] weekdayNames = new DateFormatSymbols().getShortWeekdays();
do {
System.out.printf("%4s", weekdayNames[weekday]);
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}
while (weekday != firstDayOfWeek);
System.out.println(); for (int i = 1; i <= indent; i++)
System.out.print(" "); d.set(Calendar.DAY_OF_MONTH, 1);
do {
// print day
int day = d.get(Calendar.DAY_OF_MONTH);
System.out.printf("%3d", day); // current day with *
if (day == today) System.out.print("*");
else System.out.print(" "); // advance d to the next day
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK); // start a new line at the start of the week
if (weekday == firstDayOfWeek) System.out.println();
}
while (d.get(Calendar.MONTH) == month);
// the loop exits when d is day 1 of the next month // print final end of line if necessary
if (weekday != firstDayOfWeek) System.out.println();
}
}运行下上面的代码,对你有帮助的!
import java.util.*;public class Calendar {
public static void main(String[] args) {
int years, months, day_1, day_2, sum_1, sum_2, sum, firstDay;
boolean testYear;
System.out.print("\n");
System.out.print("*********欢迎使用万年历查询时间*********");
System.out.println("\n");
System.out.print("请输入年份:");
Scanner input = new Scanner(System.in);
years = input.nextInt();
day_1 = 0;
day_2 = 0;
sum_1 = 0;
sum_2 = 0;
sum = 0;
firstDay = 0;
if ((years % 4 == 0 && years % 100 != 0) || years % 400 == 0) {
System.out.println("\t" + years + "是闰年");
testYear = true;
} else {
System.out.println("\t" + years + "是平年");
testYear = false;
}
for (int i = 1900; i < years; i++) {
if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0) {
day_1 = 366;
} else {
day_1 = 365;
}
sum_1 = sum_1 + day_1; // 计算从输入年份1月1号到1900年1月1号总天数
}
System.out.println();
System.out.print("请输入月份:");
months = input.nextInt();
/*
* 计算输入的月份到该年份的天数
*/
for (int j = 1; j < months; j++) {
if (j == 2) {
if (testYear) {
sum_2 = 29 + sum_2;
} else {
sum_2 = 28 + sum_2;
}
} else if ((j == 4) || (j == 6) || (j == 9) || (j == 11)) {
sum_2 = 30 + sum_2;
} else {
sum_2 = 31 + sum_2;
}
}
if (months == 2) {
if (testYear) {
day_2 = 29;
} else {
day_2 = 28;
}
} else if ((months == 4) || (months == 6) || (months == 9)
|| (months == 11)) {
day_2 = 30;
} else {
day_2 = 31;
}
sum = sum_1 + sum_2; // 累加记录距离1900年1月1号的天数
System.out.print("\n\n");
System.out.println("星期天" + "\t星期一" + "\t星期二" + "\t星期三" + "\t星期四"
+ "\t星期五" + "\t星期六" + "\n");
int temp = (sum+1) % 7; // 计算该月份的第一天是星期几
/** 周一-周六:使用数字1-6表示,周日用0表示 */
// if (temp == 0) {
// firstDay = 1;
// } else {
// firstDay = temp;
// }
firstDay = temp;
for (int nullNo = 0; nullNo < firstDay; nullNo++) {
System.out.print("\t"); // 循环输出第一行的空格
}
for (int x = 1; x <= day_2; x++) {
System.out.print(x+"\t");
if((x+firstDay) % 7 == 0){
System.out.println();
}
}
}
}