展开全部
import java.util.Calendar;
public class DateBean
{
private Calendar calendar = Calendar.getInstance();
public DateBean(int year, int month, int date){
calendar.set(year,month-1,date);
}
/*
*获取月份中的第一个的星期数
*1-7星期日到星期六
*@return int
*/
public int getFistWeek(){
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
calendar.set(year,month,1);
return calendar.get(Calendar.DAY_OF_WEEK);
}
/*
*当月最大天数
*@return int
*/
public int getMaxDate(){
int maxDate = calendar.getActualMaximum(Calendar.DATE);
return maxDate;
}
public void showCalendar(){
System.out.println("日\t一\t二\t三\t四\t五\t六");
int min_week = getFistWeek();
int maxDate = getMaxDate();
int tempDate = 0;
//共有几周
int rowsCount = (maxDate-(7-min_week))/7;
if((maxDate-(7-min_week))%7 == 0){
rowsCount += 1;
}else{
rowsCount += 2;
}
for(int i=0; i<rowsCount; i++){
int j = 0;
if(i==0){
j = min_week-1;
for(int o=0; o<min_week-1; o++){
System.out.print(" \t");
}
}
for(; j<7; j++){
if(tempDate>=maxDate){
break;
}
tempDate++;
System.out.print(tempDate+"\t");
}
System.out.println();
}
}
public static void main(String[] args){
DateBean dateBean = new DateBean(2010,11,19);
dateBean.showCalendar();
}
}
这个修改一下应该可以!!!
public class DateBean
{
private Calendar calendar = Calendar.getInstance();
public DateBean(int year, int month, int date){
calendar.set(year,month-1,date);
}
/*
*获取月份中的第一个的星期数
*1-7星期日到星期六
*@return int
*/
public int getFistWeek(){
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
calendar.set(year,month,1);
return calendar.get(Calendar.DAY_OF_WEEK);
}
/*
*当月最大天数
*@return int
*/
public int getMaxDate(){
int maxDate = calendar.getActualMaximum(Calendar.DATE);
return maxDate;
}
public void showCalendar(){
System.out.println("日\t一\t二\t三\t四\t五\t六");
int min_week = getFistWeek();
int maxDate = getMaxDate();
int tempDate = 0;
//共有几周
int rowsCount = (maxDate-(7-min_week))/7;
if((maxDate-(7-min_week))%7 == 0){
rowsCount += 1;
}else{
rowsCount += 2;
}
for(int i=0; i<rowsCount; i++){
int j = 0;
if(i==0){
j = min_week-1;
for(int o=0; o<min_week-1; o++){
System.out.print(" \t");
}
}
for(; j<7; j++){
if(tempDate>=maxDate){
break;
}
tempDate++;
System.out.print(tempDate+"\t");
}
System.out.println();
}
}
public static void main(String[] args){
DateBean dateBean = new DateBean(2010,11,19);
dateBean.showCalendar();
}
}
这个修改一下应该可以!!!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询