2
\$\begingroup\$

I have written this method for converting a date time zone. How do i reduce the execution time of this method further.

public static Timestamp convertTimeZone(final Timestamp fromDate, final TimeZone fromTZ, final TimeZone toTZ ){
 Long timeInDate = fromDate.getTime() ;
 int fromOffset = fromTZ.getOffset(timeInDate);
 int toOffset = toTZ.getOffset(timeInDate);
 Timestamp dateStamp = new Timestamp(fromDate.getTime());
 if( fromOffset >= 0){
 int diff = 0;
 if( toOffset > 0){
 diff = ( fromOffset - toOffset);
 }else{
 diff = ( fromOffset + Math.abs(toOffset));
 }
 long date = fromDate.getTime() - diff;
 dateStamp.setTime( date );
 }else{
 int diff = 0;
 if( toOffset > 0){
 diff = ( Math.abs( fromOffset) + toOffset);
 }else{
 diff = ( Math.abs( fromOffset) - Math.abs(toOffset));
 }
 long date = fromDate.getTime() + diff;
 dateStamp.setTime( date );
 } 
 return dateStamp;
 }
asked Jul 21, 2011 at 6:04
\$\endgroup\$
4
  • 3
    \$\begingroup\$ What is the ultimate purpose of this method? Dates a represented as milliseconds from the beginning of the epoch. For printing you can choose the timezone of the output using SimpleDateFormat. For persistence you could save the timezone along with the timestamp. \$\endgroup\$ Commented Jul 21, 2011 at 8:14
  • \$\begingroup\$ I get the value from database as timestamp, where i won't have any information of timezone. hence converting the timezone manually \$\endgroup\$ Commented Jul 21, 2011 at 12:02
  • 2
    \$\begingroup\$ NB: Date and time handling isn't very well solved in Java. AFAIK there are several "simplifications" that can lead to problems. If you need reliable time handling have a look at the Joda Time library: joda-time.sourceforge.net . However I can't say if it will be faster than your code. \$\endgroup\$ Commented Jul 21, 2011 at 13:28
  • \$\begingroup\$ Where does the Timestamp you are using come from? Is this a java.security.Timestamp? Do you need it it be one of those? \$\endgroup\$ Commented Jul 23, 2011 at 20:44

1 Answer 1

3
\$\begingroup\$

The method can be shortened a bit. It'll probably have some effect on perfomance as well.

public static Timestamp convertTimeZone(final Timestamp fromDate, final TimeZone fromTZ, final TimeZone toTZ ){
 // primitive long should be enough for his task
 final long timeInDate = fromDate.getTime();
 final int fromOffset = fromTZ.getOffset(timeInDate);
 final int toOffset = toTZ.getOffset(timeInDate);
 return new Timestamp(timeInDate + (toOffset - fromOffset));
}
answered Jul 21, 2011 at 19:04
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.