本文整理汇总了Java中java.text.Format.Field方法的典型用法代码示例。如果您正苦于以下问题:Java Format.Field方法的具体用法?Java Format.Field怎么用?Java Format.Field使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.text.Format
的用法示例。
在下文中一共展示了Format.Field方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getCalendarField
import java.text.Format; //导入方法依赖的package包/类
/**
* Returns the calendarField under the start of the selection, or -1 if
* there is no valid calendar field under the selection (or the spinner
* isn't editing dates.
*/
private int getCalendarField(JSpinner spinner)
{
JComponent editor = spinner.getEditor();
if( editor instanceof JSpinner.DateEditor )
{
JSpinner.DateEditor dateEditor = (JSpinner.DateEditor) editor;
JFormattedTextField ftf = dateEditor.getTextField();
int start = ftf.getSelectionStart();
JFormattedTextField.AbstractFormatter formatter = ftf.getFormatter();
if( formatter instanceof InternationalFormatter )
{
Format.Field[] fields = ((InternationalFormatter) formatter).getFields(start);
for( int counter = 0; counter < fields.length; counter++ )
{
if( fields[counter] instanceof DateFormat.Field )
{
int calendarField;
if( fields[counter] == DateFormat.Field.HOUR1 )
{
calendarField = Calendar.HOUR;
}
else
{
calendarField = ((DateFormat.Field) fields[counter]).getCalendarField();
}
if( calendarField != -1 )
{
return calendarField;
}
}
}
}
}
return -1;
}
示例2: formatToCharacterIterator
import java.text.Format; //导入方法依赖的package包/类
AttributedCharacterIterator formatToCharacterIterator(Object obj, Unit unit) {
if (!(obj instanceof Number))
throw new IllegalArgumentException();
Number number = (Number) obj;
StringBuffer text = new StringBuffer();
unit.writePrefix(text);
attributes.clear();
if (obj instanceof BigInteger) {
format((BigInteger) number, text, new FieldPosition(0), true);
} else if (obj instanceof java.math.BigDecimal) {
format((java.math.BigDecimal) number, text, new FieldPosition(0)
, true);
} else if (obj instanceof Double) {
format(number.doubleValue(), text, new FieldPosition(0), true);
} else if (obj instanceof Integer || obj instanceof Long) {
format(number.longValue(), text, new FieldPosition(0), true);
} else {
throw new IllegalArgumentException();
}
unit.writeSuffix(text);
AttributedString as = new AttributedString(text.toString());
// add NumberFormat field attributes to the AttributedString
for (int i = 0; i < attributes.size(); i++) {
FieldPosition pos = attributes.get(i);
Format.Field attribute = pos.getFieldAttribute();
as.addAttribute(attribute, attribute, pos.getBeginIndex(), pos.getEndIndex());
}
// return the CharacterIterator from AttributedString
return as.getIterator();
}