java - Converting between timezones even when the same changes the date (UTC to UTC) -



java - Converting between timezones even when the same changes the date (UTC to UTC) -

i've been puzzled why next code results in alter of date 25th 24th

simpledateformat sd = new simpledateformat("dd/mm/yyyy"); datetimezone customerzone = datetimezone.fortimezone(timezone.gettimezone("utc")); datetimezone serverzone = datetimezone.utc; date date = sd.parse("25/05/2014"); datetime source = new datetime(date).withzone(customerzone).withhourofday(5); localdatetime ldate = new localdatetime(source, serverzone); system.out.println(ldate.todate()); //expected sat may 25 05:00:00

result "sat may 24 05:00:00 sast 2014"

you haven't set time zone simpledateformat, it's defaulting environment's time zone, guessing "africa/johannesburg", since have sast in result.

so when part:

simpledateformat sd = new simpledateformat("dd/mm/yyyy"); date date = sd.parse("25/05/2014");

the date object midnight in sast, 10pm prior eve in utc. rest follows there, since you're working utc point forward.

also, @ end, phone call todate, results in date object. when output that, 1 time again local time zone influencing result.

you consider calling settimezone on simpledateformat object. @ to the lowest degree origin part right. should utilize format method output final string.

however, improve solution utilize jodatime's datetimeformatter instead. won't have utilize simpledateformat or date @ all.

java datetime timezone jodatime

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -