java - UTC-N to Local Time -
using joda, how format utc+/-n time, "wall time" displayed user:
from (utc+/-n):
2015-05-15t03:28:49.523-04:00
to (est) wall:
2015-05-14 23:22:44
update (1)
please consider following code. need use timestamp writes , db in utc. in mind:
datetimezone.setdefault(datetimezone.utc); localdatetime utcdate = new localdatetime(); datetimezone utctz = datetimezone.fortimezone(timezone.gettimezone("etc/utc")); datetimezone localtz = datetimezone.fortimezone(timezone.gettimezone("america/montreal")); datetimeformatter fmt = datetimeformat.forpattern("yyyy-mm-dd't'hh:mm:ss.sssz"); fmt.withzone(localtz); datetime localdatetime = utcdate.todatetime(localtz); datetime utcdatetime = localdatetime.todatetime(utctz); timestamp u = new timestamp(utcdatetime.getmillis()); system.out.println("utc time: " + u); localdatetime date = new localdatetime(u); datetime srcdatetime = date.todatetime(utctz); datetime dstdatetime = srcdatetime.todatetime(localtz); system.out.println("utc+/- time: " + dstdatetime.tostring()); datetime datetimeintargettimezone = dstdatetime.withzone(localtz); system.out.println("wall time: " + datetimeintargettimezone.tostring("yyy-mm-dd hh:mm:ss"));
now, when extracting utc time db in timestamp object, need display time end user in "wall/funeral time", whatever want call it, in tz.
output
utc time: 2015-05-15 20:03:47.561 "good" utc+/- time: 2015-05-15t20:03:47.561-04:00 "good" wall time: 2015-05-15 20:03:47 "no! no! no! danger! we'll late!"
what in name! have dstdatetime
equal time see on wall (ie, 2015-05-15 4:03:47).
update (2)
got rid of timestamp:
datetimezone utctz = datetimezone.fortimezone(timezone.gettimezone("etc/utc")); datetimezone localtz = datetimezone.fortimezone(timezone.gettimezone("america/montreal")); datetimeformatter fmt = datetimeformat.forpattern("yyy-mm-dd hh:mm:ss"); localdatetime utcdate = new localdatetime(utctz); datetime utcdatetime = utcdate.todatetime(utctz); system.out.println("utc time: " + utcdatetime); datetime dstdatetime = utcdatetime.todatetime(localtz); system.out.println("unformated wall time: " + dstdatetime); system.out.println("wall time: " + dstdatetime.tostring(fmt));
output
utc time: 2015-05-20t14:09:28.469z unformated wall time: 2015-05-20t10:09:28.469-04:00 wall time: 2015-05-20 10:09:28
everything looks perfect however, when try right utz date db, need convert timestamp (ie, new timestamp(o.getorderdate().getmillis())), , rights local time db, , not utc zulu time need.
thanks in advance,
nick.
@nick
i'm not sure if understood question correctly can try this:
//sample input string timestamp = "2015-05-15t03:28:49.523-04:00"; //format parse datetimeformatter datetimef = datetimeformat.forpattern("yyyy-mm-dd't'hh:mm:ss.sssz"); //parse local date time localdatetime datetime = datetimef.parselocaldatetime(timestamp).minushours(4); //output here minus 4 hours system.out.println(datetime);
you can improve implementation sample code above , make offset hours more dynamic far code provides result mentioned in example.
goodluck!
Comments
Post a Comment