用java(用calendar类)写一个万年历,输入年并且显示当年的日历

显示是分两列,谢谢... 显示是分两列,谢谢 展开
 我来答
hitzsf
推荐于2017-05-17 · TA获得超过2060个赞
知道大有可为答主
回答量:1741
采纳率:78%
帮助的人:1169万
展开全部
public class MyCalendar {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        Calendar c = Calendar.getInstance();
        System.out.println("请输入数字年:(比如 2014)");
        int year = sc.nextInt();
        sc.close();
//      int year = 2014;
        c.set(Calendar.YEAR,year);
        
        
        for (int i = 0; i < 12; i++) {
         c.set(Calendar.MONTH,i);  //
         c.set(Calendar.DATE,1);  //设置成1月
         printMonth(c);

        
    }
    
    public static void printMonth(Calendar c){
     c.set(Calendar.DAY_OF_MONTH,1);   //设置成一日
     System.out.printf("\n\n=========  %s  月  =========\n",c.get(Calendar.MONTH)+1);
        String[] weeks = { "日", "一" ,  "二",  "三",  "四",  "五",  "六" };
        for (int i = 0; i < weeks.length; i++) {
            System.out.printf("%s" + (i != 6 ?"\t":"\n"),weeks[i]);
        }
         
        int offday = c.get(Calendar.DAY_OF_WEEK) - 1;
        
        for(int i = 0; i < offday; i++){
            System.out.printf("\t");
        }
         
        int month = c.get(Calendar.MONTH);
        while(c.get(Calendar.MONTH) == month ){
            System.out.printf("%d" + ( (c.get(Calendar.DAY_OF_WEEK)) != 7 ? "\t":"\n") ,c.get(Calendar.DAY_OF_MONTH));
            c.add(Calendar.DAY_OF_MONTH, 1);
        }
    }
}
匿名用户
2014-04-13
展开全部
下面是我两年前写的一个方法,_UID为空就行。类你自己建

/**
* 日历的生成代码
* @param _year
* @param _month
* @param _uid
* @return
* @throws ParseException
*/
public String getCalendar(String _year,String _month,String _uid) throws ParseException{

/* 说明:
* date = df.parse("2006-11-01");
* 只需要修改上面的日期
*/
String rtn = null;
Date() date = null;
int day = 0;
int days = 0;

Date()Format df = new SimpleDate()Format("yyyy-MM-dd");
Calendar c = Calendar.getInstance();

date = df.parse(_year+"-"+_month+"-01");
c.setTime(date);

day =date.getDay();
Date dt = new Date();
days = c.getActualMaximum(Calendar.DAY_OF_MONTH);

rtn = "<table width=100% align=center border=1 cellspacing=0 cellpadding=3 bordercolor=#6699CC style=border-collapse:collapse>\n";

rtn = rtn + "\t<tr bgcolor=#66ccff>\n";
rtn = rtn + "\t\t<td align=center>星期日</td>\n";
rtn = rtn + "\t\t<td align=center>星期一</td>\n";
rtn = rtn + "\t\t<td align=center>星期二</td>\n";
rtn = rtn + "\t\t<td align=center>星期三</td>\n";
rtn = rtn + "\t\t<td align=center>星期四</td>\n";
rtn = rtn + "\t\t<td align=center>星期五</td>\n";
rtn = rtn + "\t\t<td align=center>星期六</td>\n";
rtn = rtn + "\t</tr>\n";

rtn = rtn + "\t<tr>\n";

int i=0;
for(;i<day;i++){
if(i%7 ==0 && i != 0){
rtn = rtn + "\t</tr>\n";
rtn = rtn + "\t<tr>\n";
}
rtn = rtn + "\t\t<td>";
rtn = rtn + " ";
rtn = rtn + "</td>\n";
}

for(i=day;i<(days+day);i++){
if(i%7 ==0){
rtn = rtn + "\t</tr>\n";
rtn = rtn + "\t<tr bgcolor=#ffffcc>\n";
}
rtn = rtn + "\t\t<td valign=top>";
if(df.format(dt).equals(_year+"-"+_month+"-"+((i-day+1)<10?"0"+String.valueOf(i-day+1):String.valueOf(i-day+1)))){
rtn = rtn + "<font size=2 color=\"#FF0000\"><b>" + String.valueOf((i-day+1)) + "</b></font>";
}else{
rtn = rtn + "<font size=2>"+String.valueOf((i-day+1))+"</font>";
}

rtn = rtn + "</td>\n";
}

for(i=(days+day);i<35;i++){
if(i%7 ==0){
rtn = rtn + "\t</tr>\n";
rtn = rtn + "\t<tr>\n";
}
rtn = rtn + "\t\t<td>";
rtn = rtn + " ";
rtn = rtn + "</td>\n";
}
rtn = rtn + "\t</tr>\n";
rtn = rtn + "</table>";

c.clear();

return rtn;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式