import javax.swing.JOptionPane;
public class NewClass{
public static void main(String[] args){
int year,month;
Calender cal=new Calender(2008,10);
cal.showCalender();
year=Integer.parseInt(JOptionPane.showInputDialog("Year:"));
month=Integer.parseInt(JOptionPane.showInputDialog("Month:"));
cal.setYear(year);
cal.setMonth(month);
cal.showCalender();
}
}
class Calender{
private int year,month;
public Calender(){
year=0;
month=1;
}
public Calender(int year){
this.year=year;
month=1;
}
public Calender(int year,int month){
this.year=year;
if(month>12)
this.month=month%12;
else
this.month=month;
}
public void setYear(int year){
this.year=year;
}
public void setMonth(int month){
if(month>12)
this.month=month%12;
else
this.month=month;
}
private int dayOfMonth(){
int days=0;
switch(month){
case 1:days=31;break;
case 2:{
if(((year%4==0)&&(year%100!=0))||(year%400==0))
days=29;
else
days=28;
break;
}
case 3:days=31;break;
case 4:days=30;break;
case 5:days=31;break;
case 6:days=30;break;
case 7:days=31;break;
case 8:days=31;break;
case 9:days=30;break;
case 10:days=31;break;
case 11:days=30;break;
case 12:days=31;break;
default:
days=0;
}
return days;
}
private int dayOfWeek(){
int Y=year;
int M=month;
int D=1;
int A;
A = Y>0?(5+(Y+1)+(Y-1)/4-(Y-1)/100+(Y-1)/400)%7:(5+Y+Y/4-Y/100+Y/400)%7;
A = M>2?(A+2*(M+1)+3*(M+1)/5)%7:(A+2*(M+2)+3*(M+2)/5)%7;
if (((Y%4 == 0 && Y%100 != 0)|| Y%400 == 0) && M>2) A =(A+1)%7;
A=(A+D)%7;
return A;
}
public void showCalender(){
String str=new String();
str=" ";
str+=year+"年"+month+"月";
str+="\n\n";
str+="日 一 二 三 四 五 六\n";
int week=this.dayOfWeek();
for(int i=0,j=1;i<7;i++){
if(i
else{
str+=" "+j+" ";
j++;
}
}
str+="\n";
end:
for(int i=7-week+1;i<=this.dayOfMonth();){
for(int j=0;j<7;j++){
if(i<10)
str+=" "+i+" ";
else
str+=i+" ";
i++;
if(i>this.dayOfMonth())
break end;
}
str+="\n";
}
JOptionPane.showMessageDialog(null,str);
}
}
用一个类来实现
买本JAVASCRIPT编程的本。仔细看一下,做简单的日历是最基础的。只要用心看了,就一定会编了,而且对以后编程也有好处!
我自己写了一个,你自己好好学习学习理解吧
import java.util.Scanner;
class CalendarExample
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int year,month,day;
System.out.print("输入年份:");
year=in.nextInt();
System.out.print("输入月份:");
month=in.nextInt();
System.out.print("输入日子:");
day=in.nextInt();
Calendar rili=Calendar.getInstance();
System.out.println("\t日\t一\t二\t三\t四\t五\t六");
rili.set(year,month,day);//设置日期将日历翻到指定日期
int xingqi=rili.get(Calendar.DAY_OF_WEEK)-1;//记录星期是星期几,如果是1就是日,7为六
String a[]=new String[xingqi+31];//存放号码的一维数组
for(int i=0;i
a[i]="**";
}
for(int i=xingqi,n=1;i
if(n<=9)
{
a[i]=String.valueOf(n);
}
else
{
a[i]=String.valueOf(n);
}
n++;
}
for(int i=0;i
if(i%7==0)
{
System.out.println("");//换行
}
System.out.print("\t"+a[i]);
}
}
}