首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

setFirstDayOfWeek in Calendar 不起作用,失灵,不能用

2012-08-31 
setFirstDayOfWeek in Calendar 不起作用,失效,不能用在使用Calendar的时候,往往因为国外和中国的习惯不同

setFirstDayOfWeek in Calendar 不起作用,失效,不能用

在使用Calendar的时候,往往因为国外和中国的习惯不同,而造成迥异。比如,老外习惯周日作为每周的起始第一天,而中国习惯用周一作为每周的起始第一天。

我看见Calendar的API里面有?setFirstDayOfWeek()。

所以我设置setFirstDayOfWeek(Calendar.MONDAY)

但是发现使用cal.get(Calendar.DAY_OF_WEEK)得到的,还是以周日为起始第一天的结果。很是郁闷。上网找了,老外也有遇到这个问题的。老外遇到的问题:

?

?


    Compare this to the API for?WEEK_OF_MONTH?and?WEEK_OF_YEAR, which?do?say that they depend on the first day of the week. You can?test?to see if this works correctly for your purposes.

    If you really need a number representing day of week with 1 meaning Monday and 7 meaning Sunday, you can get it with a tiny bit of math:
    public int getDayOfWeek(String dateString){SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");try {Date date = format.parse(dateString);Calendar cal = Calendar.getInstance();cal.setTime(date);cal.setFirstDayOfWeek(Calendar.MONDAY);int tmp = cal.get(Calendar.DAY_OF_WEEK) - 1;if (0 == tmp) {tmp = 7;}return tmp;} catch (ParseException e) {e.printStackTrace();return -1;}}?下面是链接:?http://www.coderanch.com/t/381293/java/java/setFirstDayOfWeek-Calendar

热点排行