java - SimpleDateFormat parse loses timezone -
code:
simpledateformat sdf = new simpledateformat("yyyy.mm.dd hh:mm:ss z"); sdf.settimezone(timezone.gettimezone("gmt")); system.out.println(new date()); try { string d = sdf.format(new date()); system.out.println(d); system.out.println(sdf.parse(d)); } catch (exception e) { e.printstacktrace(); //to change body of catch statement use file | settings | file templates. }
output:
thu aug 08 17:26:32 gmt+08:00 2013 2013.08.08 09:26:32 gmt thu aug 08 17:26:32 gmt+08:00 2013
note format()
formats date
correctly gmt, parse()
lost gmt details. know can use substring()
, work around this, reason underlying phenomenon?
here duplicate question doesn't have answers.
edit: let me put question in way, way retrieve date object in gmt?
all needed :
simpledateformat sdf = new simpledateformat("yyyy.mm.dd hh:mm:ss"); sdf.settimezone(timezone.gettimezone("gmt")); simpledateformat sdflocal = new simpledateformat("yyyy.mm.dd hh:mm:ss"); try { string d = sdf.format(new date()); system.out.println(d); system.out.println(sdflocal.parse(d)); } catch (exception e) { e.printstacktrace(); //to change body of catch statement use file | settings | file templates. }
output : dubious, want date consistent
2013.08.08 11:01:08 thu aug 08 11:01:08 gmt+08:00 2013
Comments
Post a Comment