【发布时间】:2013-03-03 13:34:46
【问题描述】:
我正在通过 java.util.Calendar 获取有关 Java 中特定日期的信息(例如一周、一个月、一年中的天数等)。在我的情况下,是否有理由为日历对象设置语言环境?我问是因为:
System.out.println(cal.get(Calendar.DAY_OF_WEEK));
今天(星期日)的返回值始终为 1,但在我们的语言环境 (cs_CZ) 中,它应该为 7。
Locale locale = new Locale("cs", "CZ");
TimeZone tz = TimeZone.getTimeZone("Europe/Prague");
Calendar cal = GregorianCalendar.getInstance(tz, locale);
cal.setTime(new Date());
// returns => 1 (but I expected 7)
System.out.println(cal.get(Calendar.DAY_OF_WEEK));
// returns => 3 - it's OK
System.out.println(cal.get(Calendar.DAY_OF_MONTH));
编辑:我可以在周日使用 1,但我必须确保无论使用的是 Locale 还是 TimeZone,这都是不变的行为。
【问题讨论】:
-
尝试退出 cal.setTime(new Date());
-
@MiguelPrz - 没有变化