date - JAVA : Get a wrong current time -
i try current time :
dateformat dateformat = new simpledateformat("yyyy/mm/dd hh:mm:ss"); calendar cal = calendar.getinstance(); string sendingdateandtime = dateformat.format(cal.gettime()).trim();
but gmt time when want system time (and not local time because software executed in several countries can use timezone object).
i need use date library , gregoriancalendar library same wrong result.
many people have same problem solution saw it's put hard code "europe" or else in timezone object.
if can you.
thankssss
---------------------------- update ------------------------------------
i tried use system.currenttimemillis() , give parameter calendar object, gmt time too
how gmt time?
calendar calendar = calendar.getinstance(timezone.gettimezone("gmt"));
and local computer time?
calendar calendar = calendar.getinstance();
dateformat default local time zone. internal format of calendar object number of milliseconds past midnight, january 1, 1970, gmt.
edited add: when run code, local time.
public static void main (string[] args) { calendar calendar = calendar.getinstance(); dateformat dateformat = new simpledateformat("yyyy/mm/dd hh:mm:ss z"); string sendingdateandtime = dateformat.format(calendar.gettime()); system.out.println(sendingdateandtime); }
when run code, gmt, although time zone still local time zone.
public static void main (string[] args) { calendar calendar = calendar.getinstance(); timezone timezone = calendar.gettimezone(); int offsetfromutc = timezone.getoffset(calendar.gettimeinmillis()); calendar.add(calendar.millisecond, -offsetfromutc); dateformat dateformat = new simpledateformat("yyyy/mm/dd hh:mm:ss z"); string sendingdateandtime = dateformat.format(calendar.gettime()); system.out.println(sendingdateandtime); }
Comments
Post a Comment