在 Java 中使用各种日历时区(不使用 Joda Time)

Working with various Calendar TimeZone in Java (without using Joda Time)(在 Java 中使用各种日历时区(不使用 Joda Time))
本文介绍了在 Java 中使用各种日历时区(不使用 Joda Time)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在寻找一种方法来根据用户输入获取不同时区的当前时间.我知道我可以使用 Joda Time!但这是唯一的方法吗?

I was looking for a way to get current time in various timezones based on an user input. I know I could use Joda Time! but is that the only way?

在 Java 中没有这样做的选项吗?我尝试了以下代码,它为所有 3 个系统输出提供了相同的输出.

Isn't there an option in Java for doing this? I tried the following code which gives the same output for all 3 sysouts.

Calendar pst = Calendar.getInstance(TimeZone.getTimeZone("PST"));
System.out.println("PST " + pst.getTime());
Calendar ist = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));
System.out.println("IST " + ist.getTime());
Calendar utc = Calendar.getInstance(TimeZone.getTimeZone("Etc/UTC"));
System.out.println("UCT " + utc.getTime());

我在这里缺少什么来获取其他时区的当前时间?

What am I missing here to get current time in other timezones?

推荐答案

是的,这将在每种情况下(或相隔毫秒)显示相同的值,因为三个日历都引用相同的即时 (尽管有执行时间),这就是 java.util.Date 所代表的全部内容.这是 Calendar.getTime() 的结果.

Yes, that would show the same value in every case (or milliseconds apart) because the three calendars all refer to the same instant in time (execution time notwithstanding) and that's all that a java.util.Date represents. That's the result of Calendar.getTime().

但是,Calendar 本身确实知道时区,这将在您使用 Calendar.get 等时反映出来.它将在您使用 SimpleDateFormat 时使用,您可以在其中指定特定时区.

However, the Calendar itself does know about time zones, and that will be reflected when you use Calendar.get etc. It will also be used when you use a SimpleDateFormat, where you can specify a particular time zone.

// Specify whatever format you want - bear in mind different locales etc
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
format.setTimeZone(calendar.getTimeZone());
String text = format.format(calendar.getTime());

目前尚不清楚您要做什么,但基本上您需要知道哪些类型是时区感知的,哪些不是.了解 java.util.Date 没有格式、日历系统或时区非常重要:它只是自 Unix 以来的毫秒数纪元.

It's not clear exactly what you're trying to do, but basically you need to be aware of which types are time zone aware, and which aren't. It's really important to understand that a java.util.Date doesn't have a format, a calendar system or a time zone: it's just the number of milliseconds since the Unix epoch.

这篇关于在 Java 中使用各种日历时区(不使用 Joda Time)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

How to implement RecyclerView with section header depending on category?(如何根据类别实现带有节标题的 RecyclerView?)
How to generate JNI header file in Eclipse(如何在 Eclipse 中生成 JNI 头文件)
Setting a custom HTTP header dynamically with Spring-WS client(使用 Spring-WS 客户端动态设置自定义 HTTP 标头)
Could you technically call the string[] anything in the main method?(从技术上讲,您可以在 main 方法中调用 string[] 吗?)
What is the proper way of setting headers in a URLConnection?(在 URLConnection 中设置标头的正确方法是什么?)
How to overwrite http-header quot;Hostquot; in a HttpURLConnection?(如何覆盖 http-header “主机在 HttpURLConnection 中?)