下面列出了java.text.DateFormatSymbols#getMonths ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Creates a new formatter.
*
* @param zone the time zone used to extract the month and year from dates
* passed to this formatter (<code>null</code> not permitted).
* @param locale the locale used to determine the month names
* (<code>null</code> not permitted).
* @param chars the maximum number of characters to use from the month
* names, or zero to indicate that the entire month name
* should be used.
* @param showYear an array of flags that control whether or not the
* year is displayed for a particular month.
* @param yearFormatter the year formatter.
*/
public MonthDateFormat(TimeZone zone, Locale locale, int chars,
boolean[] showYear, DateFormat yearFormatter) {
ParamChecks.nullNotPermitted(locale, "locale");
DateFormatSymbols dfs = new DateFormatSymbols(locale);
String[] monthsFromLocale = dfs.getMonths();
this.months = new String[12];
for (int i = 0; i < 12; i++) {
if (chars > 0) {
this.months[i] = monthsFromLocale[i].substring(0,
Math.min(chars, monthsFromLocale[i].length()));
}
else {
this.months[i] = monthsFromLocale[i];
}
}
this.calendar = new GregorianCalendar(zone);
this.showYear = showYear;
this.yearFormatter = yearFormatter;
// the following is never used, but it seems that DateFormat requires
// it to be non-null. It isn't well covered in the spec, refer to
// bug parade 5061189 for more info.
this.numberFormat = NumberFormat.getNumberInstance();
}
/**
* Initializes the locale specific month names.
*/
public void initNames() {
localInitialize = true;
DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(locale);
String[] monthNames = dateFormatSymbols.getMonths();
if (comboBox.getItemCount() == 12) {
comboBox.removeAllItems();
}
for (int i = 0; i < 12; i++) {
comboBox.addItem(monthNames[i]);
}
localInitialize = false;
comboBox.setSelectedIndex(month);
}
/**
* Creates a new formatter.
*
* @param zone the time zone used to extract the month and year from dates
* passed to this formatter (<code>null</code> not permitted).
* @param locale the locale used to determine the month names
* (<code>null</code> not permitted).
* @param chars the maximum number of characters to use from the month
* names, or zero to indicate that the entire month name
* should be used.
* @param showYear an array of flags that control whether or not the
* year is displayed for a particular month.
* @param yearFormatter the year formatter.
*/
public MonthDateFormat(TimeZone zone, Locale locale, int chars,
boolean[] showYear, DateFormat yearFormatter) {
ParamChecks.nullNotPermitted(locale, "locale");
DateFormatSymbols dfs = new DateFormatSymbols(locale);
String[] monthsFromLocale = dfs.getMonths();
this.months = new String[12];
for (int i = 0; i < 12; i++) {
if (chars > 0) {
this.months[i] = monthsFromLocale[i].substring(0,
Math.min(chars, monthsFromLocale[i].length()));
}
else {
this.months[i] = monthsFromLocale[i];
}
}
this.calendar = new GregorianCalendar(zone);
this.showYear = showYear;
this.yearFormatter = yearFormatter;
// the following is never used, but it seems that DateFormat requires
// it to be non-null. It isn't well covered in the spec, refer to
// bug parade 5061189 for more info.
this.numberFormat = NumberFormat.getNumberInstance();
}
/**
* Creates a new formatter.
*
* @param zone the time zone used to extract the month and year from dates
* passed to this formatter (<code>null</code> not permitted).
* @param locale the locale used to determine the month names
* (<code>null</code> not permitted).
* @param chars the maximum number of characters to use from the month
* names, or zero to indicate that the entire month name
* should be used.
* @param showYear an array of flags that control whether or not the
* year is displayed for a particular month.
* @param yearFormatter the year formatter.
*/
public MonthDateFormat(TimeZone zone, Locale locale, int chars,
boolean[] showYear, DateFormat yearFormatter) {
ParamChecks.nullNotPermitted(locale, "locale");
DateFormatSymbols dfs = new DateFormatSymbols(locale);
String[] monthsFromLocale = dfs.getMonths();
this.months = new String[12];
for (int i = 0; i < 12; i++) {
if (chars > 0) {
this.months[i] = monthsFromLocale[i].substring(0,
Math.min(chars, monthsFromLocale[i].length()));
}
else {
this.months[i] = monthsFromLocale[i];
}
}
this.calendar = new GregorianCalendar(zone);
this.showYear = showYear;
this.yearFormatter = yearFormatter;
// the following is never used, but it seems that DateFormat requires
// it to be non-null. It isn't well covered in the spec, refer to
// bug parade 5061189 for more info.
this.numberFormat = NumberFormat.getNumberInstance();
}
/**
* getFormattingMonthName, This returns a "formatting version" month name for the specified
* month, in the specified locale. In some languages, including Russian and Czech, the
* standalone version of the month name is different from the version of the month name you
* would use as part of a full date. (Is different from the formatting version).
*/
private static String getFormattingMonthName(Month month, Locale locale, boolean capitalize,
boolean shortVersion) {
// Get the "formatting version" of the month name.
DateFormatSymbols dateSymbols = DateFormatSymbols.getInstance(locale);
String monthName;
if (shortVersion) {
monthName = dateSymbols.getShortMonths()[month.getValue() - 1];
} else {
monthName = dateSymbols.getMonths()[month.getValue() - 1];
}
// If needed, capitalize the month name.
if ((capitalize) && (monthName != null) && (monthName.length() > 0)) {
monthName = monthName.substring(0, 1).toUpperCase(locale) + monthName.substring(1);
}
return monthName;
}
private String[] getFieldStrings(int field, int style, DateFormatSymbols symbols) {
int baseStyle = getBaseStyle(style); // ignore the standalone mask
// DateFormatSymbols doesn't support any narrow names.
if (baseStyle == NARROW_FORMAT) {
return null;
}
String[] strings = null;
switch (field) {
case ERA:
strings = symbols.getEras();
break;
case MONTH:
strings = (baseStyle == LONG) ? symbols.getMonths() : symbols.getShortMonths();
break;
case DAY_OF_WEEK:
strings = (baseStyle == LONG) ? symbols.getWeekdays() : symbols.getShortWeekdays();
break;
case AM_PM:
strings = symbols.getAmPmStrings();
break;
}
return strings;
}
public DateFormatSymbolsEx(Locale locale) {
DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(locale);
months = dateFormatSymbols.getMonths();
shortMonths = dateFormatSymbols.getShortMonths();
weekdays = dateFormatSymbols.getWeekdays();
shortWeekdays = dateFormatSymbols.getShortWeekdays();
eras = dateFormatSymbols.getEras();
ampms = dateFormatSymbols.getAmPmStrings();
}
private static String[] getDisplayNameArray(int field, boolean isLong, Locale locale) {
DateFormatSymbols dfs = new DateFormatSymbols(locale);
switch (field) {
case Calendar.AM_PM:
return dfs.getAmPmStrings();
case Calendar.DAY_OF_WEEK:
return isLong ? dfs.getWeekdays() : dfs.getShortWeekdays();
case Calendar.ERA:
return dfs.getEras();
case Calendar.MONTH:
return isLong ? dfs.getMonths() : dfs.getShortMonths();
}
return null;
}
private String[] getFieldStrings(int field, int style, DateFormatSymbols symbols) {
int baseStyle = getBaseStyle(style); // ignore the standalone mask
// DateFormatSymbols doesn't support any narrow names.
if (baseStyle == NARROW_FORMAT) {
return null;
}
String[] strings = null;
switch (field) {
case ERA:
strings = symbols.getEras();
break;
case MONTH:
strings = (baseStyle == LONG) ? symbols.getMonths() : symbols.getShortMonths();
break;
case DAY_OF_WEEK:
strings = (baseStyle == LONG) ? symbols.getWeekdays() : symbols.getShortWeekdays();
break;
case AM_PM:
strings = symbols.getAmPmStrings();
break;
}
return strings;
}
/**
* Creates a new formatter.
*
* @param zone the time zone used to extract the month and year from dates
* passed to this formatter (<code>null</code> not permitted).
* @param locale the locale used to determine the month names
* (<code>null</code> not permitted).
* @param chars the maximum number of characters to use from the month
* names, or zero to indicate that the entire month name
* should be used.
* @param showYear an array of flags that control whether or not the
* year is displayed for a particular month.
* @param yearFormatter the year formatter.
*/
public MonthDateFormat(TimeZone zone, Locale locale, int chars,
boolean[] showYear, DateFormat yearFormatter) {
if (locale == null) {
throw new IllegalArgumentException("Null 'locale' argument.");
}
DateFormatSymbols dfs = new DateFormatSymbols(locale);
String[] monthsFromLocale = dfs.getMonths();
this.months = new String[12];
for (int i = 0; i < 12; i++) {
if (chars > 0) {
months[i] = monthsFromLocale[i].substring(0,
Math.min(chars, monthsFromLocale[i].length()));
}
else {
months[i] = monthsFromLocale[i];
}
}
this.calendar = new GregorianCalendar(zone);
this.showYear = showYear;
this.yearFormatter = yearFormatter;
// the following is never used, but it seems that DateFormat requires
// it to be non-null. It isn't well covered in the spec, refer to
// bug parade 5061189 for more info.
this.numberFormat = NumberFormat.getNumberInstance();
}
public LocaledDateFormat(Locale locale) {
DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(locale);
months = dateFormatSymbols.getMonths();
shortMonths = dateFormatSymbols.getShortMonths();
weekdays = dateFormatSymbols.getWeekdays();
shortWeekdays = dateFormatSymbols.getShortWeekdays();
eras = dateFormatSymbols.getEras();
ampms = dateFormatSymbols.getAmPmStrings();
}
/**
* Parses a string (representing a month) and returns it's corresponding
* value as a Calendar month. This is used to parse MMM month dates
*
* @param monthStr
* String to parse
* @param locale
* Locale to use
* @return Month value or -1 if not found
*/
private static int getMonthForString(String monthStr, Locale locale) {
DateFormatSymbols dfs = new DateFormatSymbols(locale);
String[] months = dfs.getMonths();
for (int i = 0; i < months.length; i++) {
if (months[i].toLowerCase(locale).startsWith(monthStr.toLowerCase(locale))) {
return i + 1;
}
}
return -1;
}
private String[] getFieldStrings(int field, int style, DateFormatSymbols symbols) {
int baseStyle = getBaseStyle(style); // ignore the standalone mask
// DateFormatSymbols doesn't support any narrow names.
if (baseStyle == NARROW_FORMAT) {
return null;
}
String[] strings = null;
switch (field) {
case ERA:
strings = symbols.getEras();
break;
case MONTH:
strings = (baseStyle == LONG) ? symbols.getMonths() : symbols.getShortMonths();
break;
case DAY_OF_WEEK:
strings = (baseStyle == LONG) ? symbols.getWeekdays() : symbols.getShortWeekdays();
break;
case AM_PM:
strings = symbols.getAmPmStrings();
break;
}
return strings;
}
private String[] getFieldStrings(int field, int style, DateFormatSymbols symbols) {
int baseStyle = getBaseStyle(style); // ignore the standalone mask
// DateFormatSymbols doesn't support any narrow names.
if (baseStyle == NARROW_FORMAT) {
return null;
}
String[] strings = null;
switch (field) {
case ERA:
strings = symbols.getEras();
break;
case MONTH:
strings = (baseStyle == LONG) ? symbols.getMonths() : symbols.getShortMonths();
break;
case DAY_OF_WEEK:
strings = (baseStyle == LONG) ? symbols.getWeekdays() : symbols.getShortWeekdays();
break;
case AM_PM:
strings = symbols.getAmPmStrings();
break;
}
return strings;
}
private String[] getFieldStrings(int field, int style, DateFormatSymbols symbols) {
int baseStyle = getBaseStyle(style); // ignore the standalone mask
// DateFormatSymbols doesn't support any narrow names.
if (baseStyle == NARROW_FORMAT) {
return null;
}
String[] strings = null;
switch (field) {
case ERA:
strings = symbols.getEras();
break;
case MONTH:
strings = (baseStyle == LONG) ? symbols.getMonths() : symbols.getShortMonths();
break;
case DAY_OF_WEEK:
strings = (baseStyle == LONG) ? symbols.getWeekdays() : symbols.getShortWeekdays();
break;
case AM_PM:
strings = symbols.getAmPmStrings();
break;
}
return strings;
}
private String[] getFieldStrings(int field, int style, DateFormatSymbols symbols) {
int baseStyle = getBaseStyle(style); // ignore the standalone mask
// DateFormatSymbols doesn't support any narrow names.
if (baseStyle == NARROW_FORMAT) {
return null;
}
String[] strings = null;
switch (field) {
case ERA:
strings = symbols.getEras();
break;
case MONTH:
strings = (baseStyle == LONG) ? symbols.getMonths() : symbols.getShortMonths();
break;
case DAY_OF_WEEK:
strings = (baseStyle == LONG) ? symbols.getWeekdays() : symbols.getShortWeekdays();
break;
case AM_PM:
strings = symbols.getAmPmStrings();
break;
}
return strings;
}
/**
* setSizeOfMonthYearPanel, This sets the size of the panel at the top of the calendar that
* holds the month and the year label. The size is calculated from the largest month name (in
* pixels), that exists in locale and language that is being used by the date picker.
*/
private void setSizeOfMonthYearPanel() {
// Skip this function if the settings have not been applied.
if (settings == null) {
return;
}
// Get the font metrics object.
Font font = labelMonth.getFont();
Canvas canvas = new Canvas();
FontMetrics metrics = canvas.getFontMetrics(font);
// Calculate the preferred height for the month and year panel.
int heightNavigationButtons = buttonPreviousYear.getPreferredSize().height;
int preferredHeightMonthLabel = labelMonth.getPreferredSize().height;
int preferredHeightYearLabel = labelYear.getPreferredSize().height;
int monthFontHeight = metrics.getHeight();
int monthFontHeightWithPadding = monthFontHeight + 2;
int panelHeight = Math.max(monthFontHeightWithPadding, Math.max(preferredHeightMonthLabel,
Math.max(preferredHeightYearLabel, heightNavigationButtons)));
// Get the length of the longest translated month string (in pixels).
DateFormatSymbols symbols = DateFormatSymbols.getInstance(settings.getLocale());
String[] allLocalMonths = symbols.getMonths();
int longestMonthPixels = 0;
for (String month : allLocalMonths) {
int monthPixels = metrics.stringWidth(month);
longestMonthPixels = (monthPixels > longestMonthPixels) ? monthPixels : longestMonthPixels;
}
int yearPixels = metrics.stringWidth("_2000");
// Calculate the size of a box to hold the text with some padding.
Dimension size = new Dimension(longestMonthPixels + yearPixels + 12, panelHeight);
// Set the monthAndYearPanel to the appropriate constant size.
monthAndYearOuterPanel.setMinimumSize(size);
monthAndYearOuterPanel.setPreferredSize(size);
// monthAndYearOuterPanel.setMaximumSize(size);
// Redraw the panel.
this.doLayout();
this.validate();
}
private String[] getFieldStrings(int field, int style, DateFormatSymbols symbols) {
int baseStyle = getBaseStyle(style); // ignore the standalone mask
// DateFormatSymbols doesn't support any narrow names.
if (baseStyle == NARROW_FORMAT) {
return null;
}
String[] strings = null;
switch (field) {
case ERA:
strings = symbols.getEras();
break;
case MONTH:
strings = (baseStyle == LONG) ? symbols.getMonths() : symbols.getShortMonths();
break;
case DAY_OF_WEEK:
strings = (baseStyle == LONG) ? symbols.getWeekdays() : symbols.getShortWeekdays();
break;
case AM_PM:
strings = symbols.getAmPmStrings();
break;
}
return strings;
}
private String[] getFieldStrings(int field, int style, DateFormatSymbols symbols) {
int baseStyle = getBaseStyle(style); // ignore the standalone mask
// DateFormatSymbols doesn't support any narrow names.
if (baseStyle == NARROW_FORMAT) {
return null;
}
String[] strings = null;
switch (field) {
case ERA:
strings = symbols.getEras();
break;
case MONTH:
strings = (baseStyle == LONG) ? symbols.getMonths() : symbols.getShortMonths();
break;
case DAY_OF_WEEK:
strings = (baseStyle == LONG) ? symbols.getWeekdays() : symbols.getShortWeekdays();
break;
case AM_PM:
strings = symbols.getAmPmStrings();
break;
}
return strings;
}
public static int convertMonthToInt(char month[], DateFormatSymbols _dfs){
String months[] = _dfs.getMonths();
String shortmonths[] = _dfs.getShortMonths();
for(int i = 0; i < months.length; i++)
if(equal(month, months[i]) || equal(month, shortmonths[i]))
return i;
return -1;
}