Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将本地日期转换为GMT_Java_Date - Fatal编程技术网

Java 如何将本地日期转换为GMT

Java 如何将本地日期转换为GMT,java,date,Java,Date,上面的代码不以GMT格式打印日期,而是以本地时区打印。如何从当前日期获取GMT等效日期(假设程序可以在日本或SFO运行) 一般来说,您可以通过这种方式转换为任何(有效)时区 见 这个怎么样- DateFormat gmtFormat = new SimpleDateFormat(); TimeZone gmtTime = TimeZone.getTimeZone("GMT"); gmtFormat.setTimeZone(gmtTime); System.out.println("Curr

上面的代码不以GMT格式打印日期,而是以本地时区打印。如何从当前日期获取GMT等效日期(假设程序可以在日本或SFO运行)

一般来说,您可以通过这种方式转换为任何(有效)时区


    • 这个怎么样-

      DateFormat gmtFormat = new SimpleDateFormat();
      TimeZone gmtTime = TimeZone.getTimeZone("GMT");
      gmtFormat.setTimeZone(gmtTime);
      System.out.println("Current DateTime in GMT : " + gmtFormat.format(new Date()));
      
      测试结果
      UTC时间-2012年5月15日星期二16:24:14 IST

      GMT时间-2012年5月15日星期二10:54:14类似这样的
      SimpleDateFormat日期格式GMT=new SimpleDateFormat(“yyyy MMM dd HH:mm:ss”);
      
      dateFormatGmt.setTimeZone(TimeZone.getTimeZone(“GMT”))

      gmtFormat.format(new Date())返回类型为String。但我想输入日期。您可以
      parse()
      该字符串来创建日期实例。解析后,它将返回到您的本地时区格式。在我的例子中,它将其转换回IST。如何保持它在GMT非常感谢你。这有助于我欢迎@HaiderAli:)
      DateFormat gmtFormat = new SimpleDateFormat();
      TimeZone gmtTime = TimeZone.getTimeZone("GMT");
      gmtFormat.setTimeZone(gmtTime);
      System.out.println("Current DateTime in GMT : " + gmtFormat.format(new Date()));
      
      public static void main(String[] args) throws IOException {
          Test test=new Test();
          Date fromDate = Calendar.getInstance().getTime();
          System.out.println("UTC Time - "+fromDate);
          System.out.println("GMT Time - "+test.cvtToGmt(fromDate));
      }
      private  Date cvtToGmt( Date date ){
          TimeZone tz = TimeZone.getDefault();
          Date ret = new Date( date.getTime() - tz.getRawOffset() );
      
          // if we are now in DST, back off by the delta.  Note that we are checking the GMT date, this is the KEY.
          if ( tz.inDaylightTime( ret )){
              Date dstDate = new Date( ret.getTime() - tz.getDSTSavings() );
      
              // check to make sure we have not crossed back into standard time
              // this happens when we are on the cusp of DST (7pm the day before the change for PDT)
              if ( tz.inDaylightTime( dstDate )){
                  ret = dstDate;
              }
           }
           return ret;
      }