[英]What is purpose of locale setting in Java java.util.Calendar?
I am obtaining information (such as number of a day in week, month, year etc) about particular date in Java via java.util.Calendar. 我正在通过java.util.Calendar获取有关Java中特定日期的信息(例如,星期,月份,年中的天数等)。 Is there some reason to set locale for calendar object in my situation?
在我的情况下,是否有某些理由设置日历对象的语言环境? I am asking beacuse:
我问是因为:
System.out.println(cal.get(Calendar.DAY_OF_WEEK));
returns for today (Sunday) always number 1 but in our locale (cs_CZ) it should be 7. 今天(星期日)的返回值始终为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));
EDIT: I can hadle with 1 for Sunday, but I must be sure this is unchanging behaviour regardless to used Locale or TimeZone. 编辑:我可以在周日用1吓呆,但是无论使用Locale还是TimeZone,我都必须确保这是不变的行为。
Locale do will affect the first day of week. 区域设置会影响一周的第一天。 However, the day values are constants,
SUNDAY
is always 1. You can check this link . 但是,日期值是常量,
SUNDAY
始终为1。您可以检查此链接 。 The get()
method just returns the correct field value (If it returns 7 then it's wrong -- 7 is SATURDAY
, not the current day). get()
方法仅返回正确的字段值(如果返回7则表示错误-7是SATURDAY
,而不是当前日期)。
But you can call getFirstDayOfWeek()
and it returns 2 ( MONDAY
). 但是您可以调用
getFirstDayOfWeek()
并返回2( MONDAY
)。 I think this is what you need. 我认为这就是您所需要的。 You can take use of these two methods to reach your goal.
您可以使用这两种方法来实现您的目标。
System.out.println((cal.get(Calendar.DAY_OF_WEEK) - cal.getFirstDayOfWeek() + 7) % 7 + 1);
The above statements returns 7. 上面的语句返回7。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.