本帖最后由 kira 于 2014-2-2 17:01 编辑
各位新年好
今天复习到Date类 以及Calendar
视频中毕老师是用查表法 来定制星期的
他是这么写的
- import java.util.Calendar;
- public class CalendarDemo {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Calendar carlen =Calendar.getInstance();//获取calendar
- // System.out.println(carlen.get(Calendar.YEAR)+"年");//获取年
- String[] moon ={"一月","二月","三月","四月",
- "五月","六月","七月","八月",
- "九月","十月","十一月","十二月"};
- String[] week ={"","星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
- int index = carlen.get(Calendar.MONTH);//获取月份角标
- int indexweek = carlen.get(Calendar.WEDNESDAY);
- //以下月份写为
- System.out.println(carlen.get(Calendar.YEAR)+"年"+
- (moon[index])+
- carlen.get(Calendar.DATE)+"日"+
- carlen.get(Calendar.HOUR)+"小时"+week[indexweek]);
- }
- }
复制代码
但运行之后 发现老是前一天的 他说1角标代表星期日
我经过改动 把空值去了 因为我发现 如果把空值去掉替换成星期六的位置 结果也一样
还不是正确的星期 结果我这次直接大胆的又前推了一位
我是这么写的
- import java.util.Calendar;
- public class CalendarDemo {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Calendar carlen =Calendar.getInstance();//获取calendar
- // System.out.println(carlen.get(Calendar.YEAR)+"年");//获取年
- String[] moon ={"一月","二月","三月","四月",
- "五月","六月","七月","八月",
- "九月","十月","十一月","十二月"};
- String[] week ={"星期五","星期六","星期日","星期一","星期二","星期三","星期四",};//新写法
- int index = carlen.get(Calendar.MONTH);//获取月份角标
- int indexweek = carlen.get(Calendar.WEDNESDAY);
- //以下月份写为
- System.out.println(carlen.get(Calendar.YEAR)+"年"+
- (moon[index])+
- carlen.get(Calendar.DATE)+"日"+
- carlen.get(Calendar.HOUR)+"小时"+week[indexweek]);
- }
- }
复制代码 两处代码不同之处在于角标的获取
结果我自己写的反而是正确能获取时间的 这个是老师出现的问题么 还是什么原因
搞了半天实在没想明白 求大神解答一下{:soso_e136:}
|