How do I print a millisecond count from epoch as a date-time in UTC using Java? -


i trying use java format time in milliseconds date in utc. have following code:

long ms = 1427590800000; calendar cal = calendar.getinstance(timezone.gettimezone("utc"), locale.root); cal.settimeinmillis(ms); date date = cal.gettime(); simpledateformat dateformat = new simpledateformat("yyyy-mm-dd hh:mm:ss"); system.out.println(dateformat.format(date)); // 2015-03-29 02:00:00 

this printing time in bst (i.e. using default time-zone) rather utc. seems time-zone being set on calendar has no bearing on date being printed.

the actual time in utc shown following python snippet:

import datetime ms = 1427590800000 print datetime.datetime.utcfromtimestamp(ms/1000.0) # 2015-03-29 01:00:00 

setting default jvm time-zone "utc" results in correct date being printed, doesn't seem safe solution.

you need set timezone formatter before formatting if want desired timezone.

use dateformat.settimezone(timezone.gettimezone("utc")); , call dateformat.format(date)


Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -