java - Why the differences when converting Local Time to GMT? -
import java.text.simpledateformat; import java.util.calendar; import java.util.date; import java.util.timezone; public class defaultchecks { public static void main(string[] args) { simpledateformat dateformatgmt = new simpledateformat("yyyy-mm-dd't'hh:mm:ss.sss'z'"); calendar presentcal = calendar.getinstance(timezone.gettimezone("gmt")); system.out.println("with cal.."+dateformatgmt.format(presentcal.gettime())); dateformatgmt.settimezone(timezone.gettimezone("gmt")); string currentdatetimestring = dateformatgmt.format(new date()); system.out.println("with format.."+currentdatetimestring); } }
output:
with cal..2014-11-14t12:50:23.400z format..2014-11-14t07:20:23.400z
a date instant in time, , timezone
(s) different between 2 format calls. alter to
simpledateformat dateformatgmt = new simpledateformat( "yyyy-mm-dd't'hh:mm:ss.sss'z'"); calendar presentcal = calendar.getinstance(timezone.gettimezone("gmt")); dateformatgmt.settimezone(timezone.gettimezone("gmt")); // <-- here system.out.println("with cal.." + dateformatgmt.format(presentcal.gettime())); // <-- utilize // here. string currentdatetimestring = dateformatgmt.format(new date()); system.out.println("with format.." + currentdatetimestring);
and right output here.
java android jodatime android-calendar
No comments:
Post a Comment