import java.util.Date;
import java.util.Calendar;
import java.text.SimpleDateFormat; //日期格式类
public class MonthlyCalendar
{
public String toString() //返回当月的月历
{
Calendar now = Calendar.getInstance(); //当前日期和时间
int year =now.get(Calendar.YEAR); //年
int month=now.get(Calendar.MONTH)+1; //月
now.set(year, month-1, 1); //设置为当月1日
int week = now.get(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";
}
return str;
}
public static void main (String args[])
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日EEEEE a hh时mm分ss秒"); //定义日期格式
System.out.println(sdf.format(new Date())); //当前日期和时间
System.out.println(new MonthlyCalendar().toString());
}
}
/*
程序运行结果如下:
2009年08月19日星期三 下午 02时53分26秒
2009年8月的月历
日 一 二 三 四 五 六
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
//求当月1日是星期几,也可
int day=now.get(Calendar.DATE); //日
int week=now.get(Calendar.DAY_OF_WEEK)-1; //星期几
int first = (week - day%7 +1+7)%7; //当月1日是星期几
*/
这段代码,编译老是出现错误,帮我修改修改吧
------解决方案--------------------
你的MyDate类呢?
没有这个类肯定出错了。找找在哪里,放进去就好了。