日期:2014-05-20  浏览次数:20721 次

关于日期的问题
import   java.text.DateFormatSymbols;
import   java.text.SimpleDateFormat;
import   java.util.Calendar;

public   class   Test   {
public   static   void   main(String[]   args)   {
Test.date();
}

public   static   void   date()   {
SimpleDateFormat   o   =   new   SimpleDateFormat( "EEEE,   dd   MMM   HH:mm ");
DateFormatSymbols   dateformatsymbols   =   o.getDateFormatSymbols();
dateformatsymbols.setWeekdays(new   String[]   {   "星期天 ",   "星期一 ",星期二 ", "星期三 ", "星期四 ", "星期五 ", "星期六 ",});
dateformatsymbols.setShortWeekdays(new   String[]   {   "日 ",   "一 ",   "二 ", "三 ",   "四 ",   "五 ",   "六 "});
dateformatsymbols.setMonths(new   String[]   {   "一月 ",   "二月 ",   "三月 ",   "四月 ",   "五月 ",   "六月 ",   "七月 ",   "八月 ",   "九月 ",   "十月 ",   "十一月 ",   "十二月 "});
dateformatsymbols.setShortMonths(new   String[]   {   "第一月 ",   "第二月 ",   "第三月 ",   "第四月 ",   "第五月 ",   "第六月 ",   "第七月 ",   "第八月 ",   "第九月 ",   "第十月 ", "第十一月 ",   "第十二月 "});
o.setDateFormatSymbols(dateformatsymbols);
Calendar   calendar   =   Calendar.getInstance();
String   s   =   o.format(calendar.getTime());
System.out.println(s);
}
}
-------------
代码同上
为什么关于星期的显示结果不对呢?


------解决方案--------------------
难道星期从0开始的?
------解决方案--------------------

当改系统时间为星期六时,会按索引Calendar.SATURDAY(值为7)的下标从数组里取值,而该下标超出了该数组的最大索引.

设置的数组应按Calendar.SUNDAY,Calendar.MONDAY...这样的顺序来索引,因Calendar.SUNDAY值为1,所以应该这样设置:
dateformatsymbols.setWeekdays(new String[] { " ", "星期天 ", "星期一 ", "星期二 ", "星期三 ", "星期四 ", "星期五 ", "星期六 "});
dateformatsymbols.setShortWeekdays(new String[] { " ", "日 ", "一 ", "二 ", "三 ", "四 ", "五 ", "六 "});