Java.util.Date: try to undestand UTC and ET more -
i live in north carolina,btw, on east side. compile , run code , print out same thing. documentation java.util.date try reflect utc time.
date utctime = new date(); date esttime = new date(utctime.gettime() + timezone.gettimezone("et").getrawoffset()); dateformat format = new simpledateformat("dd/mm/yy h:mm a"); system.out.println("utc: " + format.format(utctime)); system.out.println("et: " + format.format(esttime));
and get
utc: 11/05/11 11:14 et: 11/05/11 11:14
but if go website try reflect different time, utc , et different. did wrong here
that's because getrawoffset()
returning 0 - me "et" well, , in fact timezone.gettimezone("et")
returns gmt. suspect that's not meant.
the best olson time zone name north carolina "america/new_york", believe.
note shouldn't add raw offset of time zone utc time - should set time zone of formatter instead. date
value doesn't know time zone... it's milliseconds since january 1st 1970 utc.
so can use:
import java.text.; import java.util.;
date date = new date(); dateformat format = new simpledateformat("dd/mm/yy h:mm zzz"); format.settimezone(timezone.gettimezone("america/new_york")); system.out.println("eastern: " + format.format(date)); format.settimezone(timezone.gettimezone("etc/utc")); system.out.println("utc: " + format.format(date));
output:
eastern: 11/05/11 11:30 edt utc: 11/05/11 3:30 pm utc
i'd recommend using joda time instead of built-in libraries - it's better api.
Comments
Post a Comment