3

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. :(

CristiL
  • 161
  • 1
  • 3
  • 10
  • 1
    Consider using JODA time, it might perform better. – Mister Smith Feb 23 '12 at 11:37
  • 3
    What if you cache the SimpleDateFormats? Note that they aren't thread safe, so you'll need a cache per thread. – Walter Laan Feb 23 '12 at 11:38
  • The loop calls this method. public static String parseDate(String dateFormat, TimeZone tz, String dateToConvert, String resultDateFormat, TimeZone resultTz). Also I would like to mention that the date formats are not the same, input is yyyy-MM-dd HH:mm:ss.SSS and output is yyyyMMdd-HH:mm:ss.SSS – CristiL Feb 23 '12 at 11:40

5 Answers5

9

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
Ilya
  • 29,135
  • 19
  • 110
  • 158
  • be aware, joda time is no longer recommended since *Java8*, see [joda time home page](https://www.joda.org/joda-time/) – localhost-er Feb 01 '23 at 12:23
4

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.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 14
    one should do this cautiously, as SimpleDateFormat is not thread safe (http://stackoverflow.com/questions/6840803/simpledateformat-thread-safety) – John Donn Nov 27 '14 at 09:17
3

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();
}
timaschew
  • 16,254
  • 6
  • 61
  • 78
2

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

Rinkesh
  • 3,150
  • 28
  • 32
0

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).

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228