A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 蓝枫 中级黑马   /  2014-3-28 11:22  /  952 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.*;


  2. class CalendarDemo
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.           Calendar  c = Calendar.getInstance();

  7.           c.set(2014,2,28);//2014年3月28日
  8.       c.add(Calendar.DAY_OF_MONTH,-1);
  9.           

  10.           printCalendar(c);
  11.                  
  12.          
  13.         }
  14.         public static void printCalendar(Calendar c)
  15.         {
  16.         String[] mons = {"一月","二月","三月","四月",
  17.                                      "五月","六月","七月","八月",
  18.                                  "九月","十月","十一月","十二月"};

  19.                 int index = c.get(Calendar.MONTH);
  20.                
  21.         

  22.                 String[] weeks = {"","星期日","星期一","星期二",
  23.                             "星期三","星期四","星期五","星期六"};//为什么要加一个空字符串

  24.                 int index1 = c.get(Calendar.DAY_OF_WEEK);

  25.                
  26.          sop(c.get(Calendar.YEAR)+"年");
  27.          sop(mons[index]);
  28.          sop(c.get(Calendar.DAY_OF_MONTH)+"日");
  29.          sop(weeks[index1]);
  30.     }
  31.         public static void sop(Object obj)
  32.         {
  33.                 System.out.println(obj);
  34.         }
  35. }

  36. 建立星期的字符串数组时,为什么要加上一个空的字符串,不是很理解!
复制代码

评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 神马都是浮云

查看全部评分

4 个回复

正序浏览
Calendar.DAY_OF_WEEKf 返回值在1-7之间,加入返回5,又因为周日是第一天,所以代表星期四,如果数组前面没有一个空的,组week[5]返回的是周五,就不对应了,所以加上一个空字符串
回复 使用道具 举报
也可以将37行代码改成 sop(weeks[index1-1]);那样星期数组第一个就不用加空字符串了
回复 使用道具 举报
因为index1 没有0啊
回复 使用道具 举报
因为数组以零开始,而c.get(Calendar.DAY_OF_WEEK);返回的是1到7直接的数
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马