How to format date faster than SimpleDateFormat

Hi All, Can someone tell me way to format the date faster than SimpleDateFormat.Thanks in advance. Cheers!!Puneet
[148 byte] By [puneet_ka] at [2007-11-27 8:52:18]
# 1
Do you think that Sun deliberately used a slow algorithm for SimpleDateFormat?
sabre150a at 2007-7-12 21:07:32 > top of Java-index,Java Essentials,Java Programming...
# 2

System.out.println("the date");

You couldn't leave a door like that open and not expect some to walk through it now could you...

SimpleDateFormat is quit fast enough... just don't create a new date formatter everytime you format a date... and it says so in the manual.

Keith.

corlettka at 2007-7-12 21:07:32 > top of Java-index,Java Essentials,Java Programming...
# 3
But SimpleDateFormat is not thread safe.
puneet_ka at 2007-7-12 21:07:32 > top of Java-index,Java Essentials,Java Programming...
# 4

RTFJD:

http://java.sun.com/j2se/1.5.0/docs/api/java/text/SimpleDateFormat.html

Synchronization

Date formats are not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally.

kevjavaa at 2007-7-12 21:07:32 > top of Java-index,Java Essentials,Java Programming...
# 5
> Can someone tell me way to format the date faster than SimpleDateFormat.Its always faster to write your own, but its also more work. You decide what important to you. Writing and maintaing your own code. Or using existing code that is milliseconds slower.
camickra at 2007-7-12 21:07:32 > top of Java-index,Java Essentials,Java Programming...
# 6
I'd guess about 98% of the run time of SimpleDateFormat.format will be in Calendar, splitting the date into year, month, day. Given that any polishing of the function of actually assembling the date will probable yeild little or no improvement.
malcolmmca at 2007-7-12 21:07:32 > top of Java-index,Java Essentials,Java Programming...
# 7
What leads you to think the current algorithm isn't fast enough?
georgemca at 2007-7-12 21:07:32 > top of Java-index,Java Essentials,Java Programming...
# 8
> > Can someone tell me way to format the date faster> than SimpleDateFormat.> > Its always faster to write your own,Depends how good you are. :-)
jverda at 2007-7-12 21:07:32 > top of Java-index,Java Essentials,Java Programming...
# 9

> Depends how good you are. :-)

True... and if the OP needs to ask a question like this, then maybe I should take back the suggestion that the OP will be able to come up with a faster soluton.

Of course we can't even answer the OP's question as posted since we don't even know the requirement.

camickra at 2007-7-12 21:07:32 > top of Java-index,Java Essentials,Java Programming...
# 10
> Of course we can't even answer the OP's question as> posted since we don't even know the requirement.It has to be faster. ;-)
jverda at 2007-7-12 21:07:32 > top of Java-index,Java Essentials,Java Programming...
# 11

> But SimpleDateFormat is not thread safe.

> Synchronization

>

> Date formats are not synchronized. It is recommended

> to create separate format instances for each thread.

> If multiple threads access a format concurrently, it

> must be synchronized externally.

If you want to have one SimpleDateFormat per thread, consider ThreadLocal. That's what it's for.

BigDaddyLoveHandlesa at 2007-7-12 21:07:32 > top of Java-index,Java Essentials,Java Programming...
# 12
At times when we declare formatter again and again, the SimpleDateFormat's performance is less as compared to the apache's FastDateFormat... So I was trying to find out if Sun has something to solve my problem. I am using the formatting in batch process so data to process is huge.
puneet_ka at 2007-7-12 21:07:32 > top of Java-index,Java Essentials,Java Programming...
# 13

> At times when we declare formatter again and again,

> the SimpleDateFormat's performance is less as

> compared to the apache's FastDateFormat..

1) Decaring it wouldn't be what makes it slow. Instantiating it would.

2) As already stated--simply don't instantiate that many of them. Create a few and reuse them.

3) So what if the other one is faster? Have you run into actual problems with SimpleDateFormat being a bottleneck? A Ferrari is faster than my car, but it makes no significant difference for how I use a car. I would not realize any meaningful speed improvement if I had a Ferrari.

4) If the other one is faster and it's that buggerall important, then use the faster one.

>. So I was

> trying to find out if Sun has something to solve my

> problem.

You can peruse the API docs as well as anybody else.

> I am using the formatting in batch process

> so data to process is huge.

Have you found a bottleneck? Have you put a profiler on it? Have you determined that it's the parsing that's causing the problems and that creating is negligible?

jverda at 2007-7-12 21:07:32 > top of Java-index,Java Essentials,Java Programming...
# 14
> A Ferrari is faster than my carAnd a Ferrari wouldn't have got me to work any faster today, darn bridge traffic. Now a Cigarette Boat...
BigDaddyLoveHandlesa at 2007-7-12 21:07:32 > top of Java-index,Java Essentials,Java Programming...
# 15
> > A Ferrari is faster than my car> > And a Ferrari wouldn't have got me to work any faster> today, darn bridge traffic. My point exactly.> Now a Cigarette Boat...That wouldn't help me. I don't smoke.
jverda at 2007-7-21 22:48:59 > top of Java-index,Java Essentials,Java Programming...