java Java如何在IST中打印时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10714103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:13:46  来源:igfitidea点击:

Java How to get time printed in IST

javadatetime

提问by Jon Skeet

I am able to get the time as well as timezone currently. but it always printed in

我目前能够获得时间和时区。但它总是印在

Wed May 23 11:01:08 GMT+05:30 2012

As TimeZone i am getting GMT+05:30. instead of this i want to print IST

作为时区,我得到GMT+05:30. 而不是这个我想打印IST

I tried

我试过

 SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");

but still i am getting GMT+05:30

但我仍然得到 GMT+05:30

TimeZone.getDefault().getDisplayName(true, TimeZone.SHORT); is also givine me GMT+05:30

There are various things i tried but did not get desire ouput. I have used Calenderbut still unable to print IST.

我尝试了各种各样的事情,但没有得到欲望输出。我用过Calender但还是无法打印IST

Can anyone please let me know how to print ISTin Time (even i dont want to print Indian standard time)

谁能告诉我如何及时打印IST(即使我不想打印Indian standard time

回答by Jon Skeet

You haven't shown where you're actually usingthe SimpleDateFormat. Here's a short but complete example which doesshow IST:

在那里你实际上你没有显示使用SimpleDateFormat。这是一个简短但完整的示例,它确实显示了 IST:

import java.text.*;
import java.util.*;

class Test {
    public static void main(String[] args) {
        SimpleDateFormat sd = new SimpleDateFormat(
            "yyyy.MM.dd G 'at' HH:mm:ss z");
        Date date = new Date();
        // TODO: Avoid using the abbreviations when fetching time zones.
        // Use the full Olson zone ID instead.
        sd.setTimeZone(TimeZone.getTimeZone("IST"));
        System.out.println(sd.format(date));
   }
}

Output on my machine:

我机器上的输出:

2012.05.23 AD at 11:18:08 IST

Note that if TimeZone.getDefault()isn't showing "IST" then the issue may be:

请注意,如果TimeZone.getDefault()未显示“IST”,则问题可能是:

  • That the system can't actually identify your system time zone
  • That your Java installation doesn't have full time zone information regarding i18n
  • 系统实际上无法识别您的系统时区
  • 您的 Java 安装没有关于 i18n 的完整时区信息

Which version of Java are you using, and on what platform?

您使用的是哪个版本的 Java,在什么平台上?

回答by xiaofeng.li

Seems you have a wrong timezone setting. The following code will print 2012.05.23 AD at 11:30:20 IST

似乎您的时区设置错误。以下代码将打印2012.05.23 AD at 11:30:20 IST

TimeZone.setDefault(TimeZone.getTimeZone("IST"));
SimpleDateFormat f = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
System.out.println(f.format(new Date()));

However, if I use TimeZone.setDefault(TimeZone.getTimeZone("GMT+05:30"));

但是,如果我使用 TimeZone.setDefault(TimeZone.getTimeZone("GMT+05:30"));

I'll get 2012.05.23 AD at 11:30:20 GMT+05:30.

我会得到2012.05.23 AD at 11:30:20 GMT+05:30

What comes in, comes out identical.

进来的,出来的都是一样的。