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
Post a Comment