Alternatives to SimpleDateFormat for date parsing

18,198

Solution 1

Do you have a particular reason to assume that SimpleDateFormat is inefficient at parsing dates? Unless your dates have a very specific characteristic to them that lends itself to a certain optimisation, I would have thought that the JDK class will do a reasonable job of it.

That said, on the assumption that your dates aren't all distinct (unlikely with 100k), you could look into caching - populate a map with the String being passed in and the Date coming out. This will likely greatly reduce the amount of parsing required; it may or may not result in a noticeable speed-up/memory gain depending on the existing characteristics.

As an aside, creating two new SimpleDateFormats each time is likely to be very inefficient. Why not create these instances once when the class is loaded (unless the format changes by the line)? This might solve your problem in itself, if the internals of the SDF are such that it involves a lot of char[] allocation on its first run. (Remember that bizarrely date formats aren't thread-safe, though, so you might want a ThreadLocal<DateFormat> if your parsing class is used concurrently).

Solution 2

use joda-time

org.joda.time.format.DateTimeFormatter dtf = 
         org.joda.time.format.DateTimeFormat.forPattern("yyyy-MM-dd");  
    org.joda.time.DateTime date = dtf.parseDateTime(yourDate); // String like 2000-12-12
    date.withZone(yourZone); // org.joda.time.DateTimeZone

Solution 3

As a starting point, I'd reuse those SimpleDateFormat instances rather than re-creating the pair of them for each date that you need to convert.

Solution 4

Yes, joda time has realy a nice API, but user1143825 forget to set the input timeZone. And I cannot say about the memory performance, yout have to test it and compare the results.

This should work:

DateTimeFormatter sdf = DateTimeFormat.forPattern(dateFormat).withZone(tz);
try {
  DateTime theResult = sdf.parseDateTime(dateToConvert).withZone(resultTz)
  return theResult;
} catch (IllegalArgumentException e) {
  e.printStackTrace();
}

Solution 5

using java

            sample time format //31/Mar/2013:16:59:30 -0700 
            Date date = new SimpleDateFormat("dd/MMM/yyyy:HH:mm:ss Z").parse(yourTIME);
            String time= new SimpleDateFormat("dd/MMM/yyyy, HH:mm").format(date);

Using joda Library time conversion

         org.joda.time.format.DateTimeFormatter tf=org.joda.time.format.DateTimeFormat.forPattern("dd/MMM/yyyy:HH:mm:ss Z");
         org.joda.time.DateTime date = dtf.parseDateTime(time);
         String time=date.toString("dd/MMM/yyyy, HH:mm"));

this library is improved my code speed performance

using java code 14991 ms using joda library 1668 ms

Share:
18,198
CristiL
Author by

CristiL

Updated on June 04, 2022

Comments

  • CristiL
    CristiL almost 2 years

    I would really need an alternative to SimpleDateFormat, I am converting many-many Strig dates(>100k) from JST to GMT. The problem I have is that my code generates way to many char[] , as I noticed while profiling. For 150k dates, I get constant 150MB of memory used, and its not really an option. Thanks.

        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        sdf.setTimeZone(tz);
        try {
            Date theResult = sdf.parse(dateToConvert);
            SimpleDateFormat rdf = new SimpleDateFormat(resultDateFormat);
            rdf.setTimeZone(resultTz);
            return rdf.format(theResult);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    

    I can not use Joda time, so that is not an option for me. :(