<tfoot id='49KAy'></tfoot>
    • <bdo id='49KAy'></bdo><ul id='49KAy'></ul>
    1. <small id='49KAy'></small><noframes id='49KAy'>

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

        <legend id='49KAy'><style id='49KAy'><dir id='49KAy'><q id='49KAy'></q></dir></style></legend>

        如何在 Java 中转换 UTC 和本地时区

        How to convert UTC and local timezone in Java(如何在 Java 中转换 UTC 和本地时区)
          <tbody id='XCmqq'></tbody>

            • <bdo id='XCmqq'></bdo><ul id='XCmqq'></ul>

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

            • <legend id='XCmqq'><style id='XCmqq'><dir id='XCmqq'><q id='XCmqq'></q></dir></style></legend>

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

                  本文介绍了如何在 Java 中转换 UTC 和本地时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对 Java 中的时区很好奇.我想从设备获取 UTC 时间(以毫秒为单位)并发送到服务器.服务器在向用户显示时间时会将其转换为本地时区.我系统中的时区是澳大利亚/悉尼(UTC + 11:00),我在测试时区时得到以下结果:

                  I am curious about timezone in Java. I want to get UTC time in milliseconds from a device and send to server. Server will convert it to local timezone when it displays time to users. Timezone in my system is Australia/Sydney( UTC + 11:00), and I have got the result below when I tested timezone:

                  int year = 2014;
                  int month = 0;
                  int date = 14;
                  int hourOfDay = 11;
                  int minute = 12;
                  int second = 0;
                  Calendar c1 = Calendar.getInstance();
                  c1.set(year, month, date, hourOfDay, minute, second);
                      
                  SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss z");
                  System.out.println(sdf.format(c1.getTime()));
                      
                  Calendar c2 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                  c2.set(year, month, date, hourOfDay, minute, second);
                  System.out.println(sdf.format(c2.getTime()));
                  

                  输出:

                  14/01/2014 11:12:00 EST
                  14/01/2014 22:12:00 EST
                  

                  我认为我可以为 c2 设置 13/01/2014 00:12:00,因为 UTC 时间比我的晚 11 小时.日历不是按我预期的方式工作吗?

                  I thought I could have 13/01/2014 00:12:00 for c2 because UTC time is 11 hours later than mine. Does not Calendar work the way I expect?

                  您的帮助将不胜感激.

                  添加了 z 以显示时区.这让我更加困惑,因为 Mac 说它的时区是(AEDT)澳大利亚东部夏令时间,但 Java 是 EST.无论如何,结果仍然不同,因为 EST 是 UTC-5 小时.

                  Added z to display timezone. This makes me more confused because Mac says its timezone is (AEDT) Australian Eastern Daylight Time but Java is EST. Anyway still result is different because EST is UTC-5 hours.

                  推荐答案

                  您可能打算在格式化程序上设置时区,而不是日历(或者除了日历,还不是 100% 清楚您要完成什么)!用于创建人类表示的时区来自 SimpleDateFormat.当您通过调用 getTime() 将其转换回 java.util.Date 时,日历中的所有时区"信息都会丢失.

                  You probably meant to set the timezone on your formatter, not the Calendar (or in addition the the Calendar, it is not 100% clear what you mean to accomplish)! The timezone used to create the human representation comes from the SimpleDateFormat. All "timezone" information is lost from the Calendar when you convert it back into a java.util.Date by calling getTime().

                  代码:

                  Calendar c2 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                  c2.set(year, month, date, hourOfDay, minute, second);
                  System.out.println(sdf.format(c2.getTime()));
                  

                  正在打印 14/01/2014 10:12:00,因为 Syndey(格式化程序的时区)中显示的 11AM UTC 是晚上 10 点!(使用 HH 格式表示 24 小时时间)

                  is printing 14/01/2014 10:12:00 because 11AM UTC displayed in Syndey (the timezone of your formatter) is 10PM! (use HH in the format for 24 hour time)

                  这会打印出你想要做的事情:

                  This would print what it seems like you meant to do:

                  SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss z");
                  System.out.println(sdf.format(c1.getTime()));
                  
                  sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
                  System.out.println(sdf.format(c1.getTime()));
                  

                  UTC 毫秒"的概念毫无意义.毫秒数只是历史上的一个固定点,它没有与之关联的时区.我们向其添加时区以将其转换为人类可读的表示形式.

                  The concept of 'UTC milliseconds' is meaningless. A quantity of milliseconds is just a fixed point in history, it has no timezone associated with it. We add a timezone to it to convert it into human-readable representations.

                  是的,对于(美国)东部时间和(澳大利亚)东部时间都使用EST"的歧义一直是 Java 中的一个陷阱.

                  edit: Yes, the ambiguity of using 'EST' for both (US) Eastern Time and (Australian) Eastern Time has been a pitfall in Java since forever.

                  这篇关于如何在 Java 中转换 UTC 和本地时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Java Bytecode Manipulation Library Suggestions(Java 字节码操作库建议)
                  Java CLI UI-design: frameworks or libraries?(Java CLI UI 设计:框架还是库?)
                  About the use of Beans.xml configuration file in Spring Framework application(关于Spring Framework应用中Beans.xml配置文件的使用)
                  What is the difference between Spring, Struts, Hibernate, JavaServer Faces, Tapestry?(Spring、Struts、Hibernate、JavaServer Faces、Tapestry 有什么区别?)
                  Are there any android application framework like spring?(有没有像spring这样的android应用程序框架?)
                  Java Swing based game framework. Any advice?(基于 Java Swing 的游戏框架.有什么建议吗?)

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

                  • <bdo id='BFWBI'></bdo><ul id='BFWBI'></ul>

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

                              <tbody id='BFWBI'></tbody>