Using long timestamp or Joda Time
I have a leg开发者_JAVA技巧acy 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;
}
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.
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.
精彩评论