下面列出了java.text.SimpleDateFormat#setDateFormatSymbols ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void test_setDateFormatSymbolsLjava_text_DateFormatSymbols() {
// Test for method void
// java.text.SimpleDateFormat.setDateFormatSymbols(java.text.DateFormatSymbols)
SimpleDateFormat f1 = new SimpleDateFormat("a");
DateFormatSymbols symbols = new DateFormatSymbols();
symbols.setAmPmStrings(new String[] { "morning", "night" });
f1.setDateFormatSymbols(symbols);
DateFormatSymbols newSym = f1.getDateFormatSymbols();
assertTrue("Set incorrectly", newSym.equals(symbols));
assertTrue("Not a clone", f1.getDateFormatSymbols() != symbols);
String result = f1.format(new GregorianCalendar(1999, Calendar.JUNE, 12, 3, 0).getTime());
assertEquals("Incorrect symbols used", "morning", result);
symbols.setEras(new String[] { "before", "after" });
assertTrue("Identical symbols", !f1.getDateFormatSymbols().equals(symbols));
try {
f1.setDateFormatSymbols(null);
fail();
} catch (NullPointerException expected) {
}
}
public Object getDesignValue( final ExpressionRuntime runtime, final ReportElement element ) {
Object formatStringRaw = element.getAttribute( AttributeNames.Core.NAMESPACE, AttributeNames.Core.FORMAT_STRING );
final Object staticValue = ElementTypeUtils.queryStaticValue( element );
if ( staticValue instanceof Date ) {
if ( formatStringRaw == null ) {
// return the default to-string behavior of java.util.Date
formatStringRaw = DEFAULT_FORMAT;
}
final Locale locale = runtime.getResourceBundleFactory().getLocale();
final TimeZone timeZone = runtime.getResourceBundleFactory().getTimeZone();
final SimpleDateFormat dateFormat = new SimpleDateFormat( String.valueOf( formatStringRaw ), locale );
dateFormat.setDateFormatSymbols( new DateFormatSymbols( locale ) );
dateFormat.setTimeZone( timeZone );
return rotate( element, dateFormat.format( staticValue ), runtime );
}
final Object value = ElementTypeUtils.queryFieldName( element );
return rotate( element, value != null ? value : getId(), runtime );
}
static private Axis chartXAxis(int size) {
final Axis xAxis = new Axis();
xAxis.setAutoGenerated(false);
xAxis.setHasTiltedLabels(true);
xAxis.setTiltAngle(-90f);
xAxis.setMaxLabelChars(7);
SimpleDateFormat sdf = new SimpleDateFormat(DateFormat.is24HourFormat(xdrip.getAppContext()) ? "HH:mm" : "a h:mm");
DateFormatSymbols symbols = new DateFormatSymbols(Locale.getDefault());
// OVERRIDE SOME symbols WHILE RETAINING OTHERS
symbols.setAmPmStrings(new String[] { "a", "p" });
sdf.setDateFormatSymbols(symbols);
final java.text.DateFormat timeFormat = sdf;
timeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
final GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(JoH.tsl());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
xAxis.setAutoGenerated(false);
// TODO make this a better am/pm/24 hour thingy by dividing a day down? DST??? how does that work?? - done on load of value?
List<AxisValue> axisValues = new ArrayList<>();
final int step = size / segments;
final long dayStartMs = calendar.getTimeInMillis();
final long increment = Constants.DAY_IN_MS / segments;
for (int i = 0; i < size; i = i + step) {
calendar.setTimeInMillis(dayStartMs + i*increment);
axisValues.add(new AxisValue(i / step, timeFormat.format(calendar.getTimeInMillis()).toCharArray()));
}
xAxis.setValues(axisValues);
return xAxis;
}
static private Axis chartXAxis(int size) {
final Axis xAxis = new Axis();
xAxis.setAutoGenerated(false);
xAxis.setHasTiltedLabels(true);
xAxis.setTiltAngle(-90f);
xAxis.setMaxLabelChars(7);
SimpleDateFormat sdf = new SimpleDateFormat(DateFormat.is24HourFormat(xdrip.getAppContext()) ? "HH:mm" : "a h:mm");
DateFormatSymbols symbols = new DateFormatSymbols(Locale.getDefault());
// OVERRIDE SOME symbols WHILE RETAINING OTHERS
symbols.setAmPmStrings(new String[] { "a", "p" });
sdf.setDateFormatSymbols(symbols);
final java.text.DateFormat timeFormat = sdf;
timeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
final GregorianCalendar calendar = new GregorianCalendar();
calendar.setTimeInMillis(JoH.tsl());
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
xAxis.setAutoGenerated(false);
// TODO make this a better am/pm/24 hour thingy by dividing a day down? DST??? how does that work?? - done on load of value?
List<AxisValue> axisValues = new ArrayList<>();
final int step = size / segments;
final long dayStartMs = calendar.getTimeInMillis();
final long increment = Constants.DAY_IN_MS / segments;
for (int i = 0; i < size; i = i + step) {
calendar.setTimeInMillis(dayStartMs + i*increment);
axisValues.add(new AxisValue(i / step, timeFormat.format(calendar.getTimeInMillis()).toCharArray()));
}
xAxis.setValues(axisValues);
return xAxis;
}
/**
* Allows the AM/PM text to be set.
*
* @param am AM text
* @param pm PM text
*/
public void setAmPmText(String am, String pm)
{
for (SimpleDateFormat format : m_formats)
{
DateFormatSymbols symbols = format.getDateFormatSymbols();
symbols.setAmPmStrings(new String[]
{
am,
pm
});
format.setDateFormatSymbols(symbols);
}
}
protected SimpleDateFormat initialValue() {
final Locale locale = ThreadSafeDateFormatter.this.locale == null
? Locale.getDefault() : ThreadSafeDateFormatter.this.locale;
final SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);
if (tz != null) {
formatter.setTimeZone(tz);
}
if (symbols != null) {
formatter.setDateFormatSymbols(symbols);
}
return formatter;
}
private String formatDate(Locale l, String fmt, DateFormatSymbols dfs) {
SimpleDateFormat sdf = new SimpleDateFormat(fmt, l);
sdf.setDateFormatSymbols(dfs);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
return sdf.format(new Date(0));
}
/**
* Parses the value read from the column specified by the given field-name and tries to parse it into a Date using the
* given SimpleDateFormat-pattern.
*
* @return the value of the function.
*/
public Object getValue() {
final DataRow dataRow = getDataRow();
// get the row directly as a Number
final Object o = dataRow.get( field );
// check if that thing is a Number
if ( o instanceof Date ) {
return o;
}
// get a string and convert
try {
Locale localeUsed = locale;
if ( localeUsed == null ) {
localeUsed = getResourceBundleFactory().getLocale();
}
final DateFormat format;
if ( dateFormat == null || ObjectUtilities.equal( localeUsed, lastLocale ) == false ) {
final String formatString = getFormat();
if ( formatString == null || formatString.length() == 0 ) {
format = DateFormat.getDateInstance( DateFormat.DEFAULT, localeUsed );
dateFormat = format;
lastLocale = localeUsed;
} else {
final SimpleDateFormat sformat = new SimpleDateFormat( formatString );
if ( locale != null ) {
sformat.setDateFormatSymbols( new DateFormatSymbols( locale ) );
} else {
final ResourceBundleFactory factory = getResourceBundleFactory();
sformat.setDateFormatSymbols( new DateFormatSymbols( factory.getLocale() ) );
}
format = sformat;
dateFormat = sformat;
lastLocale = localeUsed;
}
} else {
format = dateFormat;
}
if ( ObjectUtilities.equal( timeZone, lastTimeZone ) == false ) {
lastTimeZone = timeZone;
format.setTimeZone( timeZone );
}
return format.parse( String.valueOf( o ) );
} catch ( ParseException e ) {
return null;
}
}