求高手解决,急,急,急!
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class yueli
{
public static void Output(int month) //返回当月的月历
{
Calendar now = Calendar.getInstance();
int year=now.get(Calendar.YEAR);
for( month=1;month<=12;month++)
{
int week = Calendar.DAY_OF_WEEK-1; //当月1日是星期几
String str=" "+year+"年"+month+"月的月历\n 日 一 二 三 四 五 六\n";
str+=String.format("%"+4*week+"c", ' '); //前导空格
int days = MyDate.daysOfMonth(year, month); //计算出当月的天数
for (int i=1; i<=days; i++)
{
str+=String.format("%4d", i);
if ((week+i)%7==0)
str+="\n";
}
System.out.println(str);
}
}
public static void main (String args[])
{
int y=0;
Output(y);
}
}
要求输出本年月历,但输出的日期和星期不对应,求怎样改才能解决
------解决方案--------------------
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class yueli
{
public static void Output(int month) //返回当月的月历
{
Calendar now = Calendar.getInstance();
int year=now.get(Calendar.YEAR);
for( month=1;month<=12;month++)
{
int week = Mydate.weekOfDay(year, month); //当月1日是星期几
String str=" "+year+"年"+month+"月的月历\n 日 一 二 三 四 五 六\n";
str+=String.format("%"+4*week+"c", ' '); //前导空格
int days = Mydate.daysOfMonth(year, month); //计算出当月的天数
for (int i=1; i<=days; i++)
{
str+=String.format("%4d", i);
if ((week+i)%7==0)
str+="\n";
}
System.out.println(str);
}
}
public static void main (String args[])
{
int y=0;
Output(y);
}
}
------解决方案--------------------
public class Mydate {
public static int daysOfMonth(int year,int month)
{
int days = 0;
if(month!=2)
{
switch(month)
{
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;
}
}
else
{
if(year%4==0 && year%100!=0 || year%400==0)
days = 29;
else days = 28;
}
return days;
}
public static int weekOfDay(int year,int month)
{
int weekOfDay = 0;
int totalDay = 0;
int dayOfYear = 0;
for(int i=1900;i<year;i++)
{
if(i%4==0&&i%100!=0||i%400==0)
{
totalDay+=366;
}
else
{ totalDay+=365;
}
}
int day = 0;
for(int i=1;i<=month;i++){
day = Mydate.daysOfMonth(year, i);
if(i<month){ dayOfYear+=day;//把用户输入的月份之前的所有天
}
}
totalDay += dayOfYear;
weekOfDay=(1+totalDay)%7;
return weekOfDay;
}
}