It is slow and so is String.format(). I've been messing around with it a bit and here is a *rough *sample I tested:
private static final String DATE_AM_PM[] = { "am", "pm" }; private static Date dFormat = new Date(); private static int iHour, iAMPM; public static String formatDate( long lTimestamp ) { dFormat.setTime( lTimestamp ); iHour = dFormat.getHours(); if( iHour == 0 ) { iHour = 12; iAMPM = 0; } if( iHour == 12 ) { iAMPM = 1; } else if( iHour > 0 && iHour < 11 ) { iAMPM = 0; } else if( iHour > 12 && iHour < 24 ) { iHour -= 12; iAMPM = 1; } return dFormat.getMonth() + "/" + dFormat.getDay() + "/" + (dFormat.getYear() + 1900) + " " + iHour + ":" + dFormat.getMinutes() + DATE_AM_PM[ iAMPM ]; } As I said, it is rough and doesn't even format the output fully. I tossed this in my application and used it a bit for a speed test and this takes up half the time as the formatting routines do. Funny thing is, it has nothing to do with the date part. It seems the formatters are using a StringBuffer and some replace functions in the StringBuffer and that is what is slowing it down. I may try to work up a JNI replacement and see if some sprintf or fprintf action fix the issue but it may not because of the extra layer. Steven Studio LFP http://www.studio-lfp.com On Thursday, October 13, 2011 4:26:32 PM UTC-5, tlegras wrote: > > Hi, > > I am optimizing the critical parts of my code, and I coming to some > ResourceCursorAdapter bindView method. > Though the method is quite long, I saw on traceview that only 2 lines are > taking 40% of the time in traceview; those are call to DateFormat: > > ((TextView)view.findViewById(R.id.EPG_list_item_hour)).setText( > android.text.format.DateFormat.format("kk:mm",beginTimeMilli) + " - " > + > android.text.format.DateFormat.format("kk:mm",endTimeMilli)); > > > So just curious: Is there any more efficient way to transform a > "timeMillisecond" time to a date string (in my case hour/minute)? > > Thierry. > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en