<small id='qzmXt'></small><noframes id='qzmXt'>

    <tfoot id='qzmXt'></tfoot>
    <i id='qzmXt'><tr id='qzmXt'><dt id='qzmXt'><q id='qzmXt'><span id='qzmXt'><b id='qzmXt'><form id='qzmXt'><ins id='qzmXt'></ins><ul id='qzmXt'></ul><sub id='qzmXt'></sub></form><legend id='qzmXt'></legend><bdo id='qzmXt'><pre id='qzmXt'><center id='qzmXt'></center></pre></bdo></b><th id='qzmXt'></th></span></q></dt></tr></i><div id='qzmXt'><tfoot id='qzmXt'></tfoot><dl id='qzmXt'><fieldset id='qzmXt'></fieldset></dl></div>

      • <bdo id='qzmXt'></bdo><ul id='qzmXt'></ul>
    1. <legend id='qzmXt'><style id='qzmXt'><dir id='qzmXt'><q id='qzmXt'></q></dir></style></legend>

        Java DateFormat parse() 不尊重时区

        时间:2023-09-29
          <tbody id='ZXTDf'></tbody>
          <legend id='ZXTDf'><style id='ZXTDf'><dir id='ZXTDf'><q id='ZXTDf'></q></dir></style></legend>

            1. <tfoot id='ZXTDf'></tfoot>

              1. <i id='ZXTDf'><tr id='ZXTDf'><dt id='ZXTDf'><q id='ZXTDf'><span id='ZXTDf'><b id='ZXTDf'><form id='ZXTDf'><ins id='ZXTDf'></ins><ul id='ZXTDf'></ul><sub id='ZXTDf'></sub></form><legend id='ZXTDf'></legend><bdo id='ZXTDf'><pre id='ZXTDf'><center id='ZXTDf'></center></pre></bdo></b><th id='ZXTDf'></th></span></q></dt></tr></i><div id='ZXTDf'><tfoot id='ZXTDf'></tfoot><dl id='ZXTDf'><fieldset id='ZXTDf'></fieldset></dl></div>

                <small id='ZXTDf'></small><noframes id='ZXTDf'>

                • <bdo id='ZXTDf'></bdo><ul id='ZXTDf'></ul>
                  本文介绍了Java DateFormat parse() 不尊重时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("America/New_York"));
                  DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
                  df.setTimeZone(TimeZone.getTimeZone("America/New_York"));
                  
                  try {
                      System.out.println(df.format(cal.getTime()));
                      System.out.println(df.parse(df.format(cal.getTime())));
                  } catch (ParseException e) {
                      e.printStackTrace();
                  }
                  

                  结果如下:

                  2011-09-24 14:10:51 -0400

                  2011-09-24 14:10:51 -0400

                  2011 年 9 月 24 日星期六 20:10:51 CEST

                  Sat Sep 24 20:10:51 CEST 2011

                  为什么当我解析从 format() 获得的日期时,它不遵守时区?

                  Why when I parse a date I get from format() it doesn't respect the timezone?

                  推荐答案

                  你正在打印调用 Date.toString()总是使用默认时区.基本上,您不应该将 Date.toString() 用于除调试以外的任何事情.

                  You're printing the result of calling Date.toString(), which always uses the default time zone. Basically, you shouldn't use Date.toString() for anything other than debugging.

                  不要忘记 Date 没有时区 - 它代表时间的瞬间,以 Unix 纪元以来的毫秒数(1 月的午夜1970 UTC).

                  Don't forget that a Date doesn't have a time zone - it represents an instant in time, measured as milliseconds since the Unix epoch (midnight on January 1st 1970 UTC).

                  如果您再次使用格式化程序格式化日期,应该会得出与以前相同的答案.

                  If you format the date using your formatter again, that should come up with the same answer as before.

                  顺便说一句,我建议使用 Joda Time 而不是 Date/Calendar 如果你在 Java 中做大量的日期/时间工作;这是一个非常更好的 API.

                  As an aside, I would recommend the use of Joda Time instead of Date/Calendar if you're doing any significant amount of date/time work in Java; it's a much nicer API.

                  这篇关于Java DateFormat parse() 不尊重时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 Java 中的外国语言环境中格式化货币 下一篇:我们可以使用 String.format() 用所需长度的字符填充

                  相关文章

                  最新文章

                    • <bdo id='pbveo'></bdo><ul id='pbveo'></ul>
                      <i id='pbveo'><tr id='pbveo'><dt id='pbveo'><q id='pbveo'><span id='pbveo'><b id='pbveo'><form id='pbveo'><ins id='pbveo'></ins><ul id='pbveo'></ul><sub id='pbveo'></sub></form><legend id='pbveo'></legend><bdo id='pbveo'><pre id='pbveo'><center id='pbveo'></center></pre></bdo></b><th id='pbveo'></th></span></q></dt></tr></i><div id='pbveo'><tfoot id='pbveo'></tfoot><dl id='pbveo'><fieldset id='pbveo'></fieldset></dl></div>
                    1. <small id='pbveo'></small><noframes id='pbveo'>

                    2. <tfoot id='pbveo'></tfoot>

                      <legend id='pbveo'><style id='pbveo'><dir id='pbveo'><q id='pbveo'></q></dir></style></legend>