Adroid's DateFormat replacement - missing the format() with FieldPosition
        Posted  
        
            by user331244
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by user331244
        
        
        
        Published on 2010-05-03T08:45:24Z
        Indexed on 
            2010/05/03
            8:48 UTC
        
        
        Read the original article
        Hit count: 341
        
dateformat
|android
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?
© Stack Overflow or respective owner