I have a list with 开发者_JAVA百科many Date Strings such as "Fri, 08 Apr 2011 22:28:00 -0400" and need to parse them to a proper format (Friday, 08. April 2011). My problem is that the device needs a very very long time parsing > 10 date objects and occasionally runs out of memory. Is there a more efficient way parsing dates as:
SimpleDateFormat sdf = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
// later in code
try {
Date date = sdf.parse(myDateString);
return DateFormat.format("dd. MMMM yyyy", date).toString();
} catch (java.text.ParseException e) {
}
How can I parse many date Strings very fast?
Try this : Alternatives to FastDateFormat for efficient date parsing?
One thing you can do is to store the output DateFormat
, like you do with the input DateFormat
, so that you don't have to create a new one every time:
DateFormat parser = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
DateFormat formatter = new SimpleDateFormat("dd. MMMM yyyy");
// later in code
try {
Date date = parser.parse(myDateString);
return formatter.format(date);
} catch (java.text.ParseException e) {}
If possible, assign this task to your database (assuming SQLite) and see if its Date and Time function are useful in your case.
精彩评论