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

2019-09-06 14:09发布

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

2条回答
欢心
2楼-- · 2019-09-06 14:48
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));
查看更多
爷、活的狠高调
3楼-- · 2019-09-06 14:48

Way cleaner in my opinion.

        TimeZone gmtTime = TimeZone.getTimeZone("IST");
        long gmtOffset = gmtTime.getOffset(new Date().getTime())/ TimeUnit.HOURS.toMillis(1);
查看更多
登录 后发表回答