Converting timezone for date faster using java -


i have written method converting date time zone. how reduce execution time of 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; } 

with joda-time may this:

datetime dt = new datetime(datetimezone.forid("gmt")); system.out.println(dt); // 5 dt = dt.withzone(datetimezone.forid("eet")); system.out.println(dt); // 8 

note timestamp has no notion of timezone not suitable representing it.

your solution have running time, since it's o(1). it's harder read though.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -