Cheok Yan Cheng
Cheok Yan Cheng

Reputation: 42700

Using long timestamp or Joda Time

I have a legacy Java application, where its performance bottle neck is due to the usage of Calendar. As Calendar is a mutable object, we have to clone every-time we get it.

public Calendar getCalendar() {
    return (Calendar)calendar.clone();
}

We also discover that in our application, we didn't use the time zone information at all. I was wondering, should we just re-factor the code to

public long getTimestamp() {
    return timestamp;
}

We will only turn the timestamp into Calendar or Joda DateTime, when we need to perform date/time arithmetic operation.

Or to prevent unforeseen future, should we use Joda DateTime?

public DateTime getDateTime() {
    return dateTime;
}

Upvotes: 3

Views: 3186

Answers (2)

mik1
mik1

Reputation: 568

Generally, if you don't care about timezone and you have to store a lot of timestamps, you'd better store them as long-s. It is 8 bytes per timestamp. All object oriented date-time wrappers around 'long' will consume at least 24 bytes per timestamp. When you'll need to perform any datetime arithmetic - convert your long timestamp into a Joda DateTime (or MutableDateTime). They are both implemented as wrappers around a 'long' field, so creating them from long is extremely cheap.

Upvotes: 4

Jim Ferrans
Jim Ferrans

Reputation: 31012

You should definitely use JodaTime. Make sure you pass around your dates in java.util.Date, which is just a long number of milliseconds since 1/1/1970 in UTC.

Upvotes: 0

Related Questions