下面列出了java.util.Calendar#ERA 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static String[] getNames(String id, int field, int style, Locale locale) {
int context = toContext(style);
int width = toWidth(style);
DateFormatSymbols symbols = getDateFormatSymbols(id, locale);
switch (field) {
case Calendar.MONTH:
return symbols.getMonths(context, width);
case Calendar.ERA:
switch (width) {
case DateFormatSymbols.NARROW:
return symbols.getNarrowEras();
case DateFormatSymbols.ABBREVIATED:
return symbols.getEras();
case DateFormatSymbols.WIDE:
return symbols.getEraNames();
default:
throw new UnsupportedOperationException("Unknown width: " + width);
}
case Calendar.DAY_OF_WEEK:
return symbols.getWeekdays(context, width);
case Calendar.AM_PM:
return symbols.getAmPmStrings();
default:
throw new UnsupportedOperationException("Unknown field: " + field);
}
}
/**
* Get the short and long values displayed for a field
* @param field The field of interest
* @return A sorted array of the field key / value pairs
*/
KeyValue[] getDisplayNames(int field) {
Integer fieldInt = Integer.valueOf(field);
KeyValue[] fieldKeyValues= nameValues.get(fieldInt);
if(fieldKeyValues==null) {
DateFormatSymbols symbols= DateFormatSymbols.getInstance(locale);
switch(field) {
case Calendar.ERA:
// DateFormatSymbols#getEras() only returns AD/BC or translations
// It does not work for the Thai Buddhist or Japanese Imperial calendars.
// see: https://issues.apache.org/jira/browse/TRINIDAD-2126
Calendar c = Calendar.getInstance(locale);
// N.B. Some calendars have different short and long symbols, e.g. ja_JP_JP
String[] shortEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.SHORT, locale));
String[] longEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.LONG, locale));
fieldKeyValues= createKeyValues(longEras, shortEras);
break;
case Calendar.DAY_OF_WEEK:
fieldKeyValues= createKeyValues(symbols.getWeekdays(), symbols.getShortWeekdays());
break;
case Calendar.AM_PM:
fieldKeyValues= createKeyValues(symbols.getAmPmStrings(), null);
break;
case Calendar.MONTH:
fieldKeyValues= createKeyValues(symbols.getMonths(), symbols.getShortMonths());
break;
default:
throw new IllegalArgumentException("Invalid field value "+field);
}
KeyValue[] prior = nameValues.putIfAbsent(fieldInt, fieldKeyValues);
if(prior!=null) {
fieldKeyValues= prior;
}
}
return fieldKeyValues;
}
/**
* Get the short and long values displayed for a field
* @param field The field of interest
* @return A sorted array of the field key / value pairs
*/
KeyValue[] getDisplayNames(int field) {
Integer fieldInt = Integer.valueOf(field);
KeyValue[] fieldKeyValues= nameValues.get(fieldInt);
if(fieldKeyValues==null) {
DateFormatSymbols symbols= DateFormatSymbols.getInstance(locale);
switch(field) {
case Calendar.ERA:
// DateFormatSymbols#getEras() only returns AD/BC or translations
// It does not work for the Thai Buddhist or Japanese Imperial calendars.
// see: https://issues.apache.org/jira/browse/TRINIDAD-2126
Calendar c = Calendar.getInstance(locale);
// N.B. Some calendars have different short and long symbols, e.g. ja_JP_JP
String[] shortEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.SHORT, locale));
String[] longEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.LONG, locale));
fieldKeyValues= createKeyValues(longEras, shortEras);
break;
case Calendar.DAY_OF_WEEK:
fieldKeyValues= createKeyValues(symbols.getWeekdays(), symbols.getShortWeekdays());
break;
case Calendar.AM_PM:
fieldKeyValues= createKeyValues(symbols.getAmPmStrings(), null);
break;
case Calendar.MONTH:
fieldKeyValues= createKeyValues(symbols.getMonths(), symbols.getShortMonths());
break;
default:
throw new IllegalArgumentException("Invalid field value "+field);
}
KeyValue[] prior = nameValues.putIfAbsent(fieldInt, fieldKeyValues);
if(prior!=null) {
fieldKeyValues= prior;
}
}
return fieldKeyValues;
}
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;
}
/**
* Gets the text for the specified chrono, field, locale and style
* for the purpose of formatting.
* <p>
* The text associated with the value is returned.
* The null return value should be used if there is no applicable text, or
* if the text would be a numeric representation of the value.
*
* @param chrono the Chronology to get text for, not null
* @param field the field to get text for, not null
* @param value the field value to get text for, not null
* @param style the style to get text for, not null
* @param locale the locale to get text for, not null
* @return the text for the field value, null if no text found
*/
public String getText(Chronology chrono, TemporalField field, long value,
TextStyle style, Locale locale) {
if (chrono == IsoChronology.INSTANCE
|| !(field instanceof ChronoField)) {
return getText(field, value, style, locale);
}
int fieldIndex;
int fieldValue;
if (field == ERA) {
fieldIndex = Calendar.ERA;
if (chrono == JapaneseChronology.INSTANCE) {
if (value == -999) {
fieldValue = 0;
} else {
fieldValue = (int) value + 2;
}
} else {
fieldValue = (int) value;
}
} else if (field == MONTH_OF_YEAR) {
fieldIndex = Calendar.MONTH;
fieldValue = (int) value - 1;
} else if (field == DAY_OF_WEEK) {
fieldIndex = Calendar.DAY_OF_WEEK;
fieldValue = (int) value + 1;
if (fieldValue > 7) {
fieldValue = Calendar.SUNDAY;
}
} else if (field == AMPM_OF_DAY) {
fieldIndex = Calendar.AM_PM;
fieldValue = (int) value;
} else {
return null;
}
return CalendarDataUtility.retrieveJavaTimeFieldValueName(
chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
}
/**
* Get the short and long values displayed for a field
* @param field The field of interest
* @return A sorted array of the field key / value pairs
*/
KeyValue[] getDisplayNames(int field) {
Integer fieldInt = Integer.valueOf(field);
KeyValue[] fieldKeyValues= nameValues.get(fieldInt);
if(fieldKeyValues==null) {
DateFormatSymbols symbols= DateFormatSymbols.getInstance(locale);
switch(field) {
case Calendar.ERA:
// DateFormatSymbols#getEras() only returns AD/BC or translations
// It does not work for the Thai Buddhist or Japanese Imperial calendars.
// see: https://issues.apache.org/jira/browse/TRINIDAD-2126
Calendar c = Calendar.getInstance(locale);
// N.B. Some calendars have different short and long symbols, e.g. ja_JP_JP
String[] shortEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.SHORT, locale));
String[] longEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.LONG, locale));
fieldKeyValues= createKeyValues(longEras, shortEras);
break;
case Calendar.DAY_OF_WEEK:
fieldKeyValues= createKeyValues(symbols.getWeekdays(), symbols.getShortWeekdays());
break;
case Calendar.AM_PM:
fieldKeyValues= createKeyValues(symbols.getAmPmStrings(), null);
break;
case Calendar.MONTH:
fieldKeyValues= createKeyValues(symbols.getMonths(), symbols.getShortMonths());
break;
default:
throw new IllegalArgumentException("Invalid field value "+field);
}
KeyValue[] prior = nameValues.putIfAbsent(fieldInt, fieldKeyValues);
if(prior!=null) {
fieldKeyValues= prior;
}
}
return fieldKeyValues;
}
/**
* Gets the text for the specified chrono, field, locale and style
* for the purpose of formatting.
* <p>
* The text associated with the value is returned.
* The null return value should be used if there is no applicable text, or
* if the text would be a numeric representation of the value.
*
* @param chrono the Chronology to get text for, not null
* @param field the field to get text for, not null
* @param value the field value to get text for, not null
* @param style the style to get text for, not null
* @param locale the locale to get text for, not null
* @return the text for the field value, null if no text found
*/
public String getText(Chronology chrono, TemporalField field, long value,
TextStyle style, Locale locale) {
if (chrono == IsoChronology.INSTANCE
|| !(field instanceof ChronoField)) {
return getText(field, value, style, locale);
}
int fieldIndex;
int fieldValue;
if (field == ERA) {
fieldIndex = Calendar.ERA;
if (chrono == JapaneseChronology.INSTANCE) {
if (value == -999) {
fieldValue = 0;
} else {
fieldValue = (int) value + 2;
}
} else {
fieldValue = (int) value;
}
} else if (field == MONTH_OF_YEAR) {
fieldIndex = Calendar.MONTH;
fieldValue = (int) value - 1;
} else if (field == DAY_OF_WEEK) {
fieldIndex = Calendar.DAY_OF_WEEK;
fieldValue = (int) value + 1;
if (fieldValue > 7) {
fieldValue = Calendar.SUNDAY;
}
} else if (field == AMPM_OF_DAY) {
fieldIndex = Calendar.AM_PM;
fieldValue = (int) value;
} else {
return null;
}
return CalendarDataUtility.retrieveJavaTimeFieldValueName(
chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
}
/**
* Get the short and long values displayed for a field
* @param field The field of interest
* @return A sorted array of the field key / value pairs
*/
KeyValue[] getDisplayNames(int field) {
Integer fieldInt = Integer.valueOf(field);
KeyValue[] fieldKeyValues= nameValues.get(fieldInt);
if(fieldKeyValues==null) {
DateFormatSymbols symbols= DateFormatSymbols.getInstance(locale);
switch(field) {
case Calendar.ERA:
// DateFormatSymbols#getEras() only returns AD/BC or translations
// It does not work for the Thai Buddhist or Japanese Imperial calendars.
// see: https://issues.apache.org/jira/browse/TRINIDAD-2126
Calendar c = Calendar.getInstance(locale);
// N.B. Some calendars have different short and long symbols, e.g. ja_JP_JP
String[] shortEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.SHORT, locale));
String[] longEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.LONG, locale));
fieldKeyValues= createKeyValues(longEras, shortEras);
break;
case Calendar.DAY_OF_WEEK:
fieldKeyValues= createKeyValues(symbols.getWeekdays(), symbols.getShortWeekdays());
break;
case Calendar.AM_PM:
fieldKeyValues= createKeyValues(symbols.getAmPmStrings(), null);
break;
case Calendar.MONTH:
fieldKeyValues= createKeyValues(symbols.getMonths(), symbols.getShortMonths());
break;
default:
throw new IllegalArgumentException("Invalid field value "+field);
}
KeyValue[] prior = nameValues.putIfAbsent(fieldInt, fieldKeyValues);
if(prior!=null) {
fieldKeyValues= prior;
}
}
return fieldKeyValues;
}
/**
* Get the short and long values displayed for a field
* @param field The field of interest
* @return A sorted array of the field key / value pairs
*/
KeyValue[] getDisplayNames(int field) {
Integer fieldInt = Integer.valueOf(field);
KeyValue[] fieldKeyValues= nameValues.get(fieldInt);
if(fieldKeyValues==null) {
DateFormatSymbols symbols= DateFormatSymbols.getInstance(locale);
switch(field) {
case Calendar.ERA:
// DateFormatSymbols#getEras() only returns AD/BC or translations
// It does not work for the Thai Buddhist or Japanese Imperial calendars.
// see: https://issues.apache.org/jira/browse/TRINIDAD-2126
Calendar c = Calendar.getInstance(locale);
// N.B. Some calendars have different short and long symbols, e.g. ja_JP_JP
String[] shortEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.SHORT, locale));
String[] longEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.LONG, locale));
fieldKeyValues= createKeyValues(longEras, shortEras);
break;
case Calendar.DAY_OF_WEEK:
fieldKeyValues= createKeyValues(symbols.getWeekdays(), symbols.getShortWeekdays());
break;
case Calendar.AM_PM:
fieldKeyValues= createKeyValues(symbols.getAmPmStrings(), null);
break;
case Calendar.MONTH:
fieldKeyValues= createKeyValues(symbols.getMonths(), symbols.getShortMonths());
break;
default:
throw new IllegalArgumentException("Invalid field value "+field);
}
KeyValue[] prior = nameValues.putIfAbsent(fieldInt, fieldKeyValues);
if(prior!=null) {
fieldKeyValues= prior;
}
}
return fieldKeyValues;
}
/**
* Get the short and long values displayed for a field
* @param field The field of interest
* @return A sorted array of the field key / value pairs
*/
KeyValue[] getDisplayNames(int field) {
Integer fieldInt = Integer.valueOf(field);
KeyValue[] fieldKeyValues= nameValues.get(fieldInt);
if(fieldKeyValues==null) {
DateFormatSymbols symbols= DateFormatSymbols.getInstance(locale);
switch(field) {
case Calendar.ERA:
// DateFormatSymbols#getEras() only returns AD/BC or translations
// It does not work for the Thai Buddhist or Japanese Imperial calendars.
// see: https://issues.apache.org/jira/browse/TRINIDAD-2126
Calendar c = Calendar.getInstance(locale);
// N.B. Some calendars have different short and long symbols, e.g. ja_JP_JP
String[] shortEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.SHORT, locale));
String[] longEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.LONG, locale));
fieldKeyValues= createKeyValues(longEras, shortEras);
break;
case Calendar.DAY_OF_WEEK:
fieldKeyValues= createKeyValues(symbols.getWeekdays(), symbols.getShortWeekdays());
break;
case Calendar.AM_PM:
fieldKeyValues= createKeyValues(symbols.getAmPmStrings(), null);
break;
case Calendar.MONTH:
fieldKeyValues= createKeyValues(symbols.getMonths(), symbols.getShortMonths());
break;
default:
throw new IllegalArgumentException("Invalid field value "+field);
}
KeyValue[] prior = nameValues.putIfAbsent(fieldInt, fieldKeyValues);
if(prior!=null) {
fieldKeyValues= prior;
}
}
return fieldKeyValues;
}
/**
* Gets the text for the specified chrono, field, locale and style
* for the purpose of formatting.
* <p>
* The text associated with the value is returned.
* The null return value should be used if there is no applicable text, or
* if the text would be a numeric representation of the value.
*
* @param chrono the Chronology to get text for, not null
* @param field the field to get text for, not null
* @param value the field value to get text for, not null
* @param style the style to get text for, not null
* @param locale the locale to get text for, not null
* @return the text for the field value, null if no text found
*/
public String getText(Chronology chrono, TemporalField field, long value,
TextStyle style, Locale locale) {
if (chrono == IsoChronology.INSTANCE
|| !(field instanceof ChronoField)) {
return getText(field, value, style, locale);
}
int fieldIndex;
int fieldValue;
if (field == ERA) {
fieldIndex = Calendar.ERA;
if (chrono == JapaneseChronology.INSTANCE) {
if (value == -999) {
fieldValue = 0;
} else {
fieldValue = (int) value + 2;
}
} else {
fieldValue = (int) value;
}
} else if (field == MONTH_OF_YEAR) {
fieldIndex = Calendar.MONTH;
fieldValue = (int) value - 1;
} else if (field == DAY_OF_WEEK) {
fieldIndex = Calendar.DAY_OF_WEEK;
fieldValue = (int) value + 1;
if (fieldValue > 7) {
fieldValue = Calendar.SUNDAY;
}
} else if (field == AMPM_OF_DAY) {
fieldIndex = Calendar.AM_PM;
fieldValue = (int) value;
} else {
return null;
}
return CalendarDataUtility.retrieveJavaTimeFieldValueName(
chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
}
/**
* Get the short and long values displayed for a field
* @param field The field of interest
* @return A sorted array of the field key / value pairs
*/
KeyValue[] getDisplayNames(int field) {
Integer fieldInt = Integer.valueOf(field);
KeyValue[] fieldKeyValues= nameValues.get(fieldInt);
if(fieldKeyValues==null) {
DateFormatSymbols symbols= DateFormatSymbols.getInstance(locale);
switch(field) {
case Calendar.ERA:
// DateFormatSymbols#getEras() only returns AD/BC or translations
// It does not work for the Thai Buddhist or Japanese Imperial calendars.
// see: https://issues.apache.org/jira/browse/TRINIDAD-2126
Calendar c = Calendar.getInstance(locale);
// N.B. Some calendars have different short and long symbols, e.g. ja_JP_JP
String[] shortEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.SHORT, locale));
String[] longEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.LONG, locale));
fieldKeyValues= createKeyValues(longEras, shortEras);
break;
case Calendar.DAY_OF_WEEK:
fieldKeyValues= createKeyValues(symbols.getWeekdays(), symbols.getShortWeekdays());
break;
case Calendar.AM_PM:
fieldKeyValues= createKeyValues(symbols.getAmPmStrings(), null);
break;
case Calendar.MONTH:
fieldKeyValues= createKeyValues(symbols.getMonths(), symbols.getShortMonths());
break;
default:
throw new IllegalArgumentException("Invalid field value "+field);
}
KeyValue[] prior = nameValues.putIfAbsent(fieldInt, fieldKeyValues);
if(prior!=null) {
fieldKeyValues= prior;
}
}
return fieldKeyValues;
}
/**
* Get the short and long values displayed for a field
* @param field The field of interest
* @return A sorted array of the field key / value pairs
*/
KeyValue[] getDisplayNames(int field) {
Integer fieldInt = Integer.valueOf(field);
KeyValue[] fieldKeyValues= nameValues.get(fieldInt);
if(fieldKeyValues==null) {
DateFormatSymbols symbols= DateFormatSymbols.getInstance(locale);
switch(field) {
case Calendar.ERA:
// DateFormatSymbols#getEras() only returns AD/BC or translations
// It does not work for the Thai Buddhist or Japanese Imperial calendars.
// see: https://issues.apache.org/jira/browse/TRINIDAD-2126
Calendar c = Calendar.getInstance(locale);
// N.B. Some calendars have different short and long symbols, e.g. ja_JP_JP
String[] shortEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.SHORT, locale));
String[] longEras = toArray(c.getDisplayNames(Calendar.ERA, Calendar.LONG, locale));
fieldKeyValues= createKeyValues(longEras, shortEras);
break;
case Calendar.DAY_OF_WEEK:
fieldKeyValues= createKeyValues(symbols.getWeekdays(), symbols.getShortWeekdays());
break;
case Calendar.AM_PM:
fieldKeyValues= createKeyValues(symbols.getAmPmStrings(), null);
break;
case Calendar.MONTH:
fieldKeyValues= createKeyValues(symbols.getMonths(), symbols.getShortMonths());
break;
default:
throw new IllegalArgumentException("Invalid field value "+field);
}
KeyValue[] prior = nameValues.putIfAbsent(fieldInt, fieldKeyValues);
if(prior!=null) {
fieldKeyValues= prior;
}
}
return fieldKeyValues;
}
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;
}
/**
* Gets the text for the specified chrono, field, locale and style
* for the purpose of formatting.
* <p>
* The text associated with the value is returned.
* The null return value should be used if there is no applicable text, or
* if the text would be a numeric representation of the value.
*
* @param chrono the Chronology to get text for, not null
* @param field the field to get text for, not null
* @param value the field value to get text for, not null
* @param style the style to get text for, not null
* @param locale the locale to get text for, not null
* @return the text for the field value, null if no text found
*/
public String getText(Chronology chrono, TemporalField field, long value,
TextStyle style, Locale locale) {
if (chrono == IsoChronology.INSTANCE
|| !(field instanceof ChronoField)) {
return getText(field, value, style, locale);
}
int fieldIndex;
int fieldValue;
if (field == ERA) {
fieldIndex = Calendar.ERA;
if (chrono == JapaneseChronology.INSTANCE) {
if (value == -999) {
fieldValue = 0;
} else {
fieldValue = (int) value + 2;
}
} else {
fieldValue = (int) value;
}
} else if (field == MONTH_OF_YEAR) {
fieldIndex = Calendar.MONTH;
fieldValue = (int) value - 1;
} else if (field == DAY_OF_WEEK) {
fieldIndex = Calendar.DAY_OF_WEEK;
fieldValue = (int) value + 1;
if (fieldValue > 7) {
fieldValue = Calendar.SUNDAY;
}
} else if (field == AMPM_OF_DAY) {
fieldIndex = Calendar.AM_PM;
fieldValue = (int) value;
} else {
return null;
}
return CalendarDataUtility.retrieveJavaTimeFieldValueName(
chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
}
/**
* Gets the text for the specified chrono, field, locale and style
* for the purpose of formatting.
* <p>
* The text associated with the value is returned.
* The null return value should be used if there is no applicable text, or
* if the text would be a numeric representation of the value.
*
* @param chrono the Chronology to get text for, not null
* @param field the field to get text for, not null
* @param value the field value to get text for, not null
* @param style the style to get text for, not null
* @param locale the locale to get text for, not null
* @return the text for the field value, null if no text found
*/
public String getText(Chronology chrono, TemporalField field, long value,
TextStyle style, Locale locale) {
if (chrono == IsoChronology.INSTANCE
|| !(field instanceof ChronoField)) {
return getText(field, value, style, locale);
}
int fieldIndex;
int fieldValue;
if (field == ERA) {
fieldIndex = Calendar.ERA;
if (chrono == JapaneseChronology.INSTANCE) {
if (value == -999) {
fieldValue = 0;
} else {
fieldValue = (int) value + 2;
}
} else {
fieldValue = (int) value;
}
} else if (field == MONTH_OF_YEAR) {
fieldIndex = Calendar.MONTH;
fieldValue = (int) value - 1;
} else if (field == DAY_OF_WEEK) {
fieldIndex = Calendar.DAY_OF_WEEK;
fieldValue = (int) value + 1;
if (fieldValue > 7) {
fieldValue = Calendar.SUNDAY;
}
} else if (field == AMPM_OF_DAY) {
fieldIndex = Calendar.AM_PM;
fieldValue = (int) value;
} else {
return null;
}
return CalendarDataUtility.retrieveJavaTimeFieldValueName(
chrono.getCalendarType(), fieldIndex, fieldValue, style.toCalendarStyle(), locale);
}
public static int getCalendarTypeForString(String oneChar) {
int calType = -1;
switch (oneChar.charAt(0)) {
case 'G':
calType = Calendar.ERA;
break;
case 'y':
calType = Calendar.YEAR;
break;
case 'M':
calType = Calendar.MONTH;
break;
case 'd':
calType = Calendar.DAY_OF_MONTH;
break;
case 'E':
calType = Calendar.DAY_OF_WEEK;
break;
case 'D':
calType = Calendar.DAY_OF_YEAR;
break;
case 'F':
calType = Calendar.DATE;
break;
case 'h':
calType = Calendar.HOUR;
break;
case 'm':
calType = Calendar.MINUTE;
break;
case 's':
calType = Calendar.SECOND;
break;
case 'S':
calType = Calendar.MILLISECOND;
break;
case 'w':
calType = Calendar.WEEK_OF_YEAR;
break;
case 'W':
calType = Calendar.WEEK_OF_MONTH;
break;
case 'a':
calType = Calendar.AM_PM;
break;
case 'k':
calType = Calendar.HOUR_OF_DAY;
break;
case 'K':
// ?
break;
case 'z':
calType = Calendar.ZONE_OFFSET;
break;
}
return calType;
}
/**
* Set the style of this CDateTime to work with dates and / or times as
* determined by the given pattern. This will set the fields shown in the
* text box and, if <code>DROP_DOWN</code> style is set, the fields of the
* drop down component.<br>
* This method is backed by an implementation of SimpleDateFormat, and as
* such, any string pattern which is valid for SimpleDateFormat may be used.
* Examples (US Locale):<br>
* </code>setPattern("MM/dd/yyyy h:mm a");</code><br />
* </code>setPattern("'Meeting @' h:mm a 'on' EEEE, MMM dd,
* yyyy");</code><br />
*
* @param pattern
* the pattern to use, if it is invalid, the original is restored
* @throws IllegalArgumentException
* @see SimpleDateFormat
* @see #getPattern()
* @see #setFormat(int)
*/
public void setPattern(String pattern) throws IllegalArgumentException {
this.allowedTimezones = null;
if (isOpen()) {
setOpen(false);
}
df = new SimpleDateFormat(pattern, locale);
df.setTimeZone(timezone);
if (updateFields()) {
this.pattern = pattern;
this.format = -1;
boolean wasDate = isDate;
boolean wasTime = isTime;
isDate = isTime = false;
calendarFields = new int[field.length];
for (int i = 0; i < calendarFields.length; i++) {
calendarFields[i] = getCalendarField(field[i]);
switch (calendarFields[i]) {
case Calendar.AM_PM:
case Calendar.HOUR:
case Calendar.HOUR_OF_DAY:
case Calendar.MILLISECOND:
case Calendar.MINUTE:
case Calendar.SECOND:
case Calendar.ZONE_OFFSET:
isTime = true;
break;
case Calendar.DAY_OF_MONTH:
case Calendar.DAY_OF_WEEK:
case Calendar.DAY_OF_WEEK_IN_MONTH:
case Calendar.DAY_OF_YEAR:
case Calendar.ERA:
case Calendar.MONTH:
case Calendar.WEEK_OF_MONTH:
case Calendar.WEEK_OF_YEAR:
case Calendar.YEAR:
isDate = true;
break;
default:
break;
}
}
if (checkButton() && (isDate != wasDate || isTime != wasTime)) {
if (defaultButtonImage) {
if (isDate && isTime) {
doSetButtonImage(Resources.getIconCalendarClock());
} else if (isDate) {
doSetButtonImage(Resources.getIconCalendar());
} else {
doSetButtonImage(Resources.getIconClock());
}
}
updateNullText();
}
if (checkText()) {
updateText();
}
if (isSimple()) {
disposePicker();
createPicker();
}
} else {
throw new IllegalArgumentException(
"Problem setting pattern: \"" + pattern + "\""); //$NON-NLS-1$ //$NON-NLS-2$
}
}
/**
* The Verify Event handler.<br>
* <b>EVERYTHING</b> is blocked via this handler (Event.doit is set to
* false). Depending upon the input, a course of action is determined and
* the displayed text is updated via the <code>updateText()</code> method.
* <br>
* This method implements the following logic: If the event is a paste, the
* pasted text is parsed for the entire date/time selection; if this parse
* is successful, the result is committed to the selection property and is
* displayed; otherwise, it is discarded. One-character pastes are discarded
* without parsing. When user types characters one by one, all non-digits
* are discarded (if they have effects, they have already been processed by
* other event handlers) while digits are added to
* <code>this.editField</code> without affecting the selection. Once
* <code>this.editField</code> reaches its capacity for the active field,
* its contents are attempted to be committed. If the commit is successful,
* focus switches to the next field of CDateTime. Otherwise, the contents of
* <code>this.editField</code> are discarded and the previous value of the
* selection (before user started typing in this field) is restored; focus
* stays in the same field. <b>Example:</b> if the seconds field contains
* "23", and user types 8 in the seconds field, "08" is shown on screen
* while getSelection still returns 23. If user types 9 after that, the
* field reaches its capacity, the attempt to commit 89 seconds fails, and
* 23 gets restored on screen.
*
* @param e
* the event
* @see CDateTime#updateText()
*/
void verify(Event e) {
// we don't want to reprocess the event
if (e.doit == false) {
return;
}
e.doit = false;
if (field.length == 0 || activeField == FIELD_NONE) {
return;
}
char c = e.character;
if (e.text.length() == 1 && String.valueOf(c).equals(e.text)
&& Character.isDigit(c) || e.text.length() > 1) {
if (e.text.length() == 1) {
if (editField == null) {
int cf = getCalendarField();
if (cf >= 0) {
int digits;
switch (cf) {
case Calendar.YEAR:
digits = 4;
break;
case Calendar.DAY_OF_YEAR:
digits = 3;
break;
case Calendar.AM_PM:
case Calendar.DAY_OF_WEEK:
case Calendar.ERA:
digits = 1;
break;
case Calendar.MILLISECOND:
digits = 3;
break;
default:
digits = 2;
}
editField = new EditField(digits, calendar.get(cf));
} else {
return;
}
}
if (editField.addChar(c)) {
if (commitEditField()) {
fieldNext();
} else {
editField = null;
if (selection.length > 0) {
selection[0] = calendar.getTime();
}
updateText();
}
}
if (selection.length > 0) {
selection[0] = calendar.getTime();
}
updatePicker();
} else {
try {
setSelection(df.parse(e.text));
fireSelectionChanged();
} catch (ParseException pe) {
// do nothing
}
}
}
updateText();
}
@Test
public void testDoReset() throws ParseException {
Calendar cal = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yy HH:mm");
Date date;
// parseFormat = MM/dd/yy HH:mm
// test year & week
_resetWeek = false;
_lastReset = Calendar.ERA; // used as a un-set value
date = df.parse("1/1/08 00:00"); // tuesday, so never gonna be the First the of Week
System.out.println("Testing date - " + date);
cal.setTime(date);
_tm.doReset(cal);
assertEquals(_lastReset, Calendar.YEAR);
assertFalse(_resetWeek);
// test month & week
_resetWeek = false;
_lastReset = Calendar.ERA; // used as a un-set value
date = df.parse("2/1/07 00:00");
System.out.println("Testing date - " + date);
cal.setTime(date);
_tm.doReset(cal);
assertEquals(_lastReset, Calendar.MONTH);
assertFalse(_resetWeek);
// test day & week
_resetWeek = false;
_lastReset = Calendar.ERA; // used as a un-set value
date = df.parse("2/3/07 00:00");
System.out.println("Testing date - " + date);
cal.setTime(date);
_tm.doReset(cal);
assertEquals(_lastReset, Calendar.DAY_OF_MONTH);
assertFalse(_resetWeek);
// test hour & week
_resetWeek = false;
_lastReset = Calendar.ERA; // used as a un-set value
date = df.parse("1/1/07 2:00");
System.out.println("Testing date - " + date);
cal.setTime(date);
_tm.doReset(cal);
assertEquals(_lastReset, Calendar.HOUR);
assertFalse(_resetWeek);
}