how to Convert PST to GMT(-0800) in java?

夙愿已清 提交于 2019-12-13 04:47:36

问题


Actually in my java program like the following code...

  String date1=null;
  String formate="IST";
  SimpleDateFormat sourceFormat = new SimpleDateFormat("z");

   SimpleDateFormat gmtFormat = new SimpleDateFormat("'GMT('Z')'");
   date1 = gmtFormat.format(sourceFormat.parse(formate));
   System.out.println(date1);//output GMT(+0530)

Hear it is giving Correct Value but the timezone may change like PST---- GMT(-0800) will so.

But my code alway show only GMT(+0530)

Please help me to convert timezone ACT,,PST,IST.....etc to GMT(+11:00),GMT(-08:00),GMT(+0530).......etc


回答1:


java.text.SimpleDateFormat sourceFormat = new SimpleDateFormat("z");

java.text.SimpleDateFormat gmtFormat = new SimpleDateFormat("'GMT('ZZZ')' zzzz");

java.util.Date date1 = sourceFormat.parse("IST");

TimeZone gmtTime = TimeZone.getTimeZone("IST");

gmtFormat.setTimeZone(gmtTime);

//System.out.println("Source date: " + date1);

System.out.println("   "+ gmtFormat.format(date1));



回答2:


Way cleaner in my opinion.

        TimeZone gmtTime = TimeZone.getTimeZone("IST");
        long gmtOffset = gmtTime.getOffset(new Date().getTime())/ TimeUnit.HOURS.toMillis(1);


来源:https://stackoverflow.com/questions/13285904/how-to-convert-pst-to-gmt-0800-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!