0
我需要拆分日期字符串成片,我使用Android的日期格式替换 - 缺少格式()与FieldPosition中
public final StringBuffer format (Object object, StringBuffer buffer, FieldPosition field)
从java.text.DateFormat中的类
这样做。但是,这个函数的实现非常慢,因此Android在android.text.format.DateFormat中有自己的实现。
但在我的情况下,我想提取日期字符串(年,分钟等)的不同部分。由于我需要独立于语言环境,因此我无法使用SimpleDateFormat和自定义字符串。我这样做如下:
Calendar c = ...
// find out what field to extract
int field = getField();
// Create a date string
Field calendarField = DateFormat.Field.ofCalendarField(field);
FieldPosition fieldPosition = new FieldPosition(calendarField);
StringBuffer label = new StringBuffer();
label = getDateFormat().format(c.getTime(), label, fieldPosition);
// Find the piece that we are looking for
int beginIndex = fieldPosition.getBeginIndex();
int endIndex = fieldPosition.getEndIndex();
String asString = label.substring(beginIndex, endIndex);
由于某些原因,格式()重载与FieldPosition参数不包括在android平台。任何想法如何以另一种方式做到这一点?有没有简单的方法来标记模式字符串?任何其他想法?