我目前能够获取时间和时区。但它总是打印在
Wed May 23 11:01:08 GMT+05:30 2012
作为 TimeZone,我得到 GMT+05:30
。而不是这个我想打印 IST
我试过了
SimpleDateFormat sd = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
但我仍然收到 GMT+05:30
TimeZone.getDefault().getDisplayName(true, TimeZone.SHORT); is also givine me GMT+05:30
我尝试了很多东西但没有得到想要的输出。我已经使用了 Calender
但仍然无法打印 IST
。
谁能告诉我如何及时打印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));
}
}
我机器上的输出:
2012.05.23 AD at 11:18:08 IST
请注意,如果 TimeZone.getDefault()
未显示“IST”,则问题可能是:
- 系统实际上无法识别您的系统时区
- 您的 Java 安装没有关于 i18n 的完整时区信息
您使用的是哪个版本的 Java,在什么平台上使用?
关于Java 如何在 IST 中打印时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10714103/