简体   繁体   English

Java 中的 GregorianCalendar 类

[英]GregorianCalendar Class in Java

I am trying to get current time in other time zone.我正在尝试获取其他时区的当前时间。 I used this code for this:我为此使用了此代码:

GregorianCalendar calender = new         
GregorianCalendar(TimeZone.getTimeZone("Asia/Bangkok"));
    System.out.println(calender.getTime());

But, when I am running this code, this code provides the current time in CET as the time in my local machine is in CET.但是,当我运行这段代码时,这段代码提供了 CET 中的当前时间,因为我本地机器中的时间是 CET 中的。 I am confused.我很迷惑。 Then why there is scope to provide a TimeZone in constructor?那么为什么在构造函数中有提供 TimeZone 的范围呢?

Ahh, the joys of the Java Date/Time API ...啊,Java 日期/时间 API 的乐趣......

What you want (aside from a better API, such as Joda Time) is a DateFormat .您想要的(除了更好的 API,例如 Joda Time)是DateFormat It can print dates in a time zone you specify.它可以在您指定的时区打印日期。 You don't need Calendar for that.你不需要Calendar

dateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Bangkok"));
dateFormat.format(new Date());

Calendar is for time manipulations and calculations. Calendar用于时间操作和计算。 For example "set the time to 10 AM".例如“将时间设置为上午 10 点”。 Then it needs the timezone.然后它需要时区。

When you are done with these calculations, then you can get the result by calling calendar.getTime() which returns a Date .完成这些计算后,您可以通过调用calendar.getTime()获取结果,该方法返回Date

A Date is essentially a universal timestamp (in milliseconds since 1970, with no timezone information attached or relevant). Date本质上是一个通用时间戳(自 1970 年以来以毫秒为单位,没有附加或相关的时区信息)。 If you call toString on a Date it will just print something in your default timezone.如果您在Date上调用toString ,它只会在您的默认时区打印一些内容。 For more control, use DateFormat .如需更多控制,请使用DateFormat

What you are doing right now is:你现在正在做的是:

  • Getting a calendar in Bangkok time zone获取曼谷时区的日历
  • get the Date object for this time( which is in ms since some date January 1, 1970, 00:00:00 GMT)获取此时间的 Date 对象(自 1970 年 1 月 1 日,格林威治标准时间 00:00:00 以来的某个日期以毫秒为单位)
  • print out this Date in your timezone ( Date.toString() )在您的时区打印此日期( Date.toString()

You should use a Formatter class to get the result you want.您应该使用 Formatter 类来获得您想要的结果。 eg SimpleDateFormat例如SimpleDateFormat

An alternative solution would be to use a less confusing Date/Time library.另一种解决方案是使用一个不太容易混淆的日期/时间库。 eg JodaTime or the new java.time package of Java8例如JodaTimeJava8的新java.time

tl;dr tl;博士

ZonedDateTime.now( ZoneId.of( "Asia/Bangkok" ) )

java.time时间

The legacy date-time classes you are using are simply terrible, flawed in design and in implementation, built by people who did not understand date-time handling.您使用的遗留日期时间类简直太糟糕了,在设计和实现上都有缺陷,是由不了解日期时间处理的人构建的。 Avoid those classes entirely.完全避免这些课程。

Use only the modern java.time classes defined in JSR 310 .仅使用JSR 310 中定义的现代java.time类。

ZoneId z = ZoneId.of( "Asia/Bangkok" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;

Generate text in standard ISO 8601 format, wisely extended to append the name of the time zone in square brackets.以标准ISO 8601格式生成文本,明智地扩展为在方括号中附加时区名称。

String output = zdt.toString() ;

For other formats, use DateTimeFormatter as seen on hundreds of other Questions and Answers.对于其他格式,请使用DateTimeFormatter如在数百个其他问题和答案中所见。

See this code run live at IdeOne.com .查看此代码在 IdeOne.com 上实时运行

2020-02-15T12:27:31.118127+07:00[Asia/Bangkok] 2020-02-15T12:27:31.118127+07:00[亚洲/曼谷]


Java 中的日期时间类型表,现代和传统


About java.time关于java.time

The java.time framework is built into Java 8 and later. java.time框架内置于 Java 8 及更高版本中。 These classes supplant the troublesome old legacy date-time classes such as java.util.Date , Calendar , & SimpleDateFormat .这些类取代麻烦的老传统日期时间类,如java.util.DateCalendar ,和SimpleDateFormat

To learn more, see the Oracle Tutorial .要了解更多信息,请参阅Oracle 教程 And search Stack Overflow for many examples and explanations.并在 Stack Overflow 上搜索许多示例和解释。 Specification is JSR 310 .规范是JSR 310

The Joda-Time project, now in maintenance mode , advises migration to the java.time classes.现在处于维护模式Joda-Time项目建议迁移到java.time类。

You may exchange java.time objects directly with your database.您可以直接与您的数据库交换java.time对象。 Use a JDBC driver compliant with JDBC 4.2 or later.使用符合JDBC 4.2或更高版本的JDBC 驱动程序 No need for strings, no need for java.sql.* classes.不需要字符串,不需要java.sql.*类。

Where to obtain the java.time classes?从哪里获得 java.time 类?

The ThreeTen-Extra project extends java.time with additional classes. ThreeTen-Extra项目用额外的类扩展了 java.time。 This project is a proving ground for possible future additions to java.time.该项目是未来可能添加到 java.time 的试验场。 You may find some useful classes here such as Interval , YearWeek , YearQuarter , and more .您可以在这里找到一些有用的类,比如IntervalYearWeekYearQuarter ,和更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM