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;
            }
\$\endgroup\$
  • 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\$ – Aleksi Yrttiaho Jul 21 '11 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\$ – leftrright Jul 21 '11 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\$ – RoToRa Jul 21 '11 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\$ – Martijn Jul 23 '11 at 20:44
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));
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.