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. 🙁
Do you have a particular reason to assume that
SimpleDateFormatis 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
Stringbeing passed in and theDatecoming 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 aThreadLocal<DateFormat>if your parsing class is used concurrently).