Hi,
I need to split a date string into pieces and I'm doing it using the
public final StringBuffer format (Object object, StringBuffer buffer, FieldPosition field)
from the java.text.DateFormat class. However, the implementation of this function is really slow, hence Android has an own implementation in android.text.format.DateFormat.
BUT, in my case, I want to extract the different pieces of the date string (year, minute and so on). Since I need to be locale independent, I can not use SimpleDateFormat and custom strings. I do it as follows:
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);
For some reason, the format() overload with the FieldPosition argument is not included in the android platform. Any ideas of how to do this in another way? Is there any easy way to tokenize the pattern string? Any other ideas?