如何构建具有自定义一周第一天的区域设置?
How can I construct a Locale with a custom first day of week?
我需要一个 Locale
对象,它与另一个对象完全相同,但一周的第一天不同(即:星期日而不是星期六)。
具体来说,我需要从周日开始的阿拉伯-埃及语言环境。我正在使用仅支持更改其语言环境的日历控件,因此是我的要求。
您可以创建一个 Calendar
对象并使用 calender.setFirstDayOfWeek()
方法设置第一天。
Locale locale = new Locale("ar-EG", "EG");
TimeZone timeZone = TimeZone.getTimeZone("Egypt");
Calendar calendar = GregorianCalendar.getInstance(locale);
Calendar calendar2 = GregorianCalendar.getInstance(locale);
calendar2.setFirstDayOfWeek(0);
System.out.println("Calender locale: " + locale + "\nTimeZone: "
+ timeZone.getDisplayName(locale) + "FirstDayOfTheWeek:"
+ calendar.getFirstDayOfWeek() + "\nCalender2 locale: "
+ locale + "\nTimeZone: " + timeZone.getDisplayName(locale)
+ "FirstDayOfTheWeek:" + calendar2.getFirstDayOfWeek());
Locale
对象不控制一周的第一天。
这是由 Calendar
class 通过以下方式完成的:
Locale locale = Locale.forLanguageTag("ar-EG");
Calendar calendar = Calendar.getInstance(locale);
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
根据 Gautam Jose 的回答:
Normally this would've been just fine. Thing is, the control I'm using keeps instantiating Calendar objects according to the default locale (application scope wise), so a custom locale. I actually tried reverse engineering the control and it does not provide leeway around the issue due to it using private members (i.e.: inheriting it can't help here)
如果您直接使用 Java 反射更改这些 private
成员,则不需要继承 API。
首先,检查控件的 class 以找到 Calendar
字段:
public class CalendarControl {
private Calendar calendar;
}
现在使用:
CalendarControl control; // The instance to manipulate
try {
Field field = control.getClass().getDeclaredField("calendar");
field.setAccessible(true);
field.set(control, calendar); // Pass the new object we created at top of this answer
} catch (Exception ex) {
// You must catch NoSuchFieldException and IllegalAccessException here
}
我需要一个 Locale
对象,它与另一个对象完全相同,但一周的第一天不同(即:星期日而不是星期六)。
具体来说,我需要从周日开始的阿拉伯-埃及语言环境。我正在使用仅支持更改其语言环境的日历控件,因此是我的要求。
您可以创建一个 Calendar
对象并使用 calender.setFirstDayOfWeek()
方法设置第一天。
Locale locale = new Locale("ar-EG", "EG");
TimeZone timeZone = TimeZone.getTimeZone("Egypt");
Calendar calendar = GregorianCalendar.getInstance(locale);
Calendar calendar2 = GregorianCalendar.getInstance(locale);
calendar2.setFirstDayOfWeek(0);
System.out.println("Calender locale: " + locale + "\nTimeZone: "
+ timeZone.getDisplayName(locale) + "FirstDayOfTheWeek:"
+ calendar.getFirstDayOfWeek() + "\nCalender2 locale: "
+ locale + "\nTimeZone: " + timeZone.getDisplayName(locale)
+ "FirstDayOfTheWeek:" + calendar2.getFirstDayOfWeek());
Locale
对象不控制一周的第一天。
这是由 Calendar
class 通过以下方式完成的:
Locale locale = Locale.forLanguageTag("ar-EG");
Calendar calendar = Calendar.getInstance(locale);
calendar.setFirstDayOfWeek(Calendar.SUNDAY);
根据
Normally this would've been just fine. Thing is, the control I'm using keeps instantiating Calendar objects according to the default locale (application scope wise), so a custom locale. I actually tried reverse engineering the control and it does not provide leeway around the issue due to it using private members (i.e.: inheriting it can't help here)
如果您直接使用 Java 反射更改这些 private
成员,则不需要继承 API。
首先,检查控件的 class 以找到 Calendar
字段:
public class CalendarControl {
private Calendar calendar;
}
现在使用:
CalendarControl control; // The instance to manipulate
try {
Field field = control.getClass().getDeclaredField("calendar");
field.setAccessible(true);
field.set(control, calendar); // Pass the new object we created at top of this answer
} catch (Exception ex) {
// You must catch NoSuchFieldException and IllegalAccessException here
}