0

I have a list with 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?

Hans Muff
  • 125
  • 1
  • 1
  • 5
  • 1
    Are you really sure that this is causing you problems and not some other piece of code? I have a ListView which lists TV shows. For every TV show, I parse/format a date in the form "yyyy-MM-dd HH:mm.ss" to "HH:mm". There are over 30 shows listed for today on one channel and this happens instantly - I can 'fling' scroll with no problems at all. 10 date objects should cause no problems at all. – Squonk Apr 13 '11 at 14:16

3 Answers3

0

Try this : Alternatives to FastDateFormat for efficient date parsing?

Community
  • 1
  • 1
Mark Mooibroek
  • 7,636
  • 3
  • 32
  • 53
0

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) {}
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • SimpleDateFormat is not thread safe. Here is a link to this known bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6205670 – John Kane Apr 13 '11 at 14:12
  • Thanks, I'm aware that it's not thread safe. What is your point? If this is really an issue, the solution is trivial. Just use a [`ThreadLocal`](http://developer.android.com/reference/java/lang/ThreadLocal.html). – Matt Ball Apr 13 '11 at 14:16
  • All that is shown here is just a code snippet, so the code that would be added to ensure thread safety is not visible. I just wanted to point the fact that the SimpleDateFormat class is not threadsafe. (I have ran into a surprising number instances where I have seen people use this class in an unsafe way) – John Kane Apr 13 '11 at 14:24
0

If possible, assign this task to your database (assuming SQLite) and see if its Date and Time function are useful in your case.

Priyank
  • 10,503
  • 2
  • 27
  • 25