java - Convert 9 Apr 2014 20:36:13 GMT to France timezone 9 Apr 2014 21:36:13 CEST -
i need convert 9 apr 2014 20:36:13 gmt france timezone 9 apr 2014 21:36:13 cest.
i come across function unable adapt code.
long ts = system.currenttimemillis(); date localtime = new date(ts); string format = "dd mmmm yyyy hh:mm"; simpledateformat sdf = new simpledateformat(format); // convert local time utc (works fine) sdf.settimezone(timezone.gettimezone("gmt")); date gmttime = new date(sdf.format(localtime)); system.out.println("local:" + localtime.tostring() + "," + localtime.gettime() + " --> utc time:" + gmttime.tostring() + "," + gmttime.gettime()); // **** code **** end **** // convert utc local time date fromgmt = new date(gmttime.gettime() + timezone.getdefault().getoffset(localtime.gettime())); system.out.println("utc time:" + gmttime.tostring() + "," + gmttime.gettime() + " --> local:" + fromgmt.tostring() + "-" + fromgmt.gettime());
any idea please.
the answer meno hochschild correct.
joda-time
using joda-time 2.3 library makes work easier.
string input = "9 apr 2014 20:36:13 gmt"; datetimeformatter formatter = datetimeformat.forpattern( "d mmm yyyy hh:mm:ss z" ).withlocale( locale.english ); datetimezone timezoneparis = datetimezone.forid( "europe/paris" ); datetime datetime = formatter.withzone( timezoneparis ).parsedatetime( input ); datetime datetimeutc = datetime.withzone( datetimezone.utc ); string output = formatter.withzone( timezoneparis ).print( datetime );
dump console…
system.out.println( "input: " + input ); system.out.println( "datetime: " + datetime ); system.out.println( "datetimeutc: " + datetimeutc ); system.out.println( "output: " + output );
when run…
input: 9 apr 2014 20:36:13 gmt datetime: 2014-04-09t22:36:13.000+02:00 datetimeutc: 2014-04-09t20:36:13.000z output: 9 apr 2014 22:36:13 cest
Comments
Post a Comment