本文主要是介绍java月历_用Java实现月历输出,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
import java.util.*;
public class Calendar_Month {
int week; //存储某月的第一天是周几
int []dayofP = {31,28,31,30,31,30,31,31,30,31,30,31}; //平年每月多少天
int []dayofR = {31,29,31,30,31,30,31,31,30,31,30,31}; //闰年每月多少天
int dayofM; //所要求月份的最大天数
int year,month; //用于存储年和月的数值
GregorianCalendar objC; //创建一个日历的实例
/**无参数的构造方法,获得当前时间*/
public Calendar_Month(){
objC = new GregorianCalendar();
}
//设置该月的第一天是周几,以及设置该月的最大天数
void setDay(int[] a){
//设置该月的第一天是周几
if (a.length == 0){
objC.set(Calendar.DAY_OF_MONTH,1);
week = objC.get(Calendar.DAY_OF_WEEK);
month = objC.get(Calendar.MONTH);
year = objC.get(Calendar.YEAR);
}
else{
month = a[1];
year = a[0];
objC.set(year,month-1,1);
week = objC.get(Calendar.DAY_OF_WEEK);
}
//设置该月的最大天数
if(objC.isLeapYear(year)){
dayofM = dayofR[month - 1];
}
else{
dayofM = dayofP[month - 1];
}
}
/**此方法用于显示日历*/
void display(){
System.out.println("\t\t\t " + year + " 年 " + toDaXie(month) + " 月");
System.out.println("\t日\t一\t二\t三\t四\t五\t六");
int len = week-1;
for(int i = 0; i < week-1; i++){
System.out.print("\t ");
}
for (int i = 1; i <= dayofM; i++) {
System.out.print("\t"+i);
len++;
if(len == 7){
System.out.println();
len=0;
}
}
}
public String toDaXie(int n){
switch (n) {
case 1:
return "一";
case 2:
return "二";
case 3:
return "三";
case 4:
return "四";
case 5:
return "五";
case 6:
return "六";
case 7:
return "七";
case 8:
return "八";
case 9:
return "九";
case 10:
return "十";
case 11:
return "十一";
case 12:
return "十二";
default:
return " ";
}
}
public static void main(String[] args) {
Calendar_Month objCa = new Calendar_Month();
Scanner scanner = new Scanner(System.in);
int[] a = new int[2];
a[0] = scanner.nextInt();
a[1] = scanner.nextInt();
objCa.setDay(a);
objCa.display();
}
}
输出结果为:
2010 5
2010 年 五 月
日一二三四五六
1
2345678
9101112131415
16171819202122
23242526272829
3031
附从网上找的一个程序:StdOut.print和System.out.println()一样功能,可代替
public class Main {
public static int getWeek(int yr, int mo, int day) {
int a = (14 - mo) / 12, y = yr - a, m = mo + (12 * a) - 2;
return (day + y + y / 4 - y / 100 + y / 400 + (31 * m) / 12) % 7;
}
public static int getMonth(int y, int m) {
if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8
|| m == 10 || m == 12)
return 31;
if (m == 4 || m == 6 || m == 9 || m == 11)
return 30;
if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0)
return 29;
else
return 28;
}
public static void menology() {
int year = StdIn.readInt(), month = StdIn.readInt();
String[] showMonth = { "一", "二", "三", "四", "五", "六", "七", "八", "九",
"十", "十一", "十二" };
StdOut.println(" " + year + "年" + showMonth[month - 1] + "月");
StdOut.print("日" + " " + "一" + " " + "二" + " " + "三" + " " + "四" + " "
+ "五" + " " + "六");
System.out.println();
int m = getMonth(year, month);
for (int i = 0; i < m; i++) {
int w = getWeek(year, month, (i + 1));
if (i == 0) {
for (int j = w; j > 0; j--)
StdOut.print(" ");
}
if (i < 9)
StdOut.print(" " + (i + 1) + " ");
else
StdOut.print((i + 1) + " ");
if ((w == 6) || ((i + 1) == m))
StdOut.println();
}
}
public static void main(String[] args) {
menology();
}}
分享到:
2010-05-05 11:15
浏览 2104
评论
1 楼
walc
2012-03-30
不错。可以学习一下。
这篇关于java月历_用Java实现月历输出的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!