GregorianCalendar Class in Java

痞子三分冷 提交于 2021-02-01 05:06:34

问题


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. I am confused. Then why there is scope to provide a TimeZone in constructor?


回答1:


Ahh, the joys of the Java Date/Time API ...

What you want (aside from a better API, such as Joda Time) is a DateFormat. It can print dates in a time zone you specify. You don't need Calendar for that.

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

Calendar is for time manipulations and calculations. For example "set the time to 10 AM". 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.

A Date is essentially a universal timestamp (in milliseconds since 1970, with no timezone information attached or relevant). If you call toString on a Date it will just print something in your default timezone. For more control, use DateFormat.




回答2:


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)
  • print out this Date in your timezone (Date.toString())

You should use a Formatter class to get the result you want. e.g. SimpleDateFormat

An alternative solution would be to use a less confusing Date/Time library. e.g. JodaTime or the new java.time package of Java8




回答3:


tl;dr

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.

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.

String output = zdt.toString() ;

For other formats, use DateTimeFormatter as seen on hundreds of other Questions and Answers.

See this code run live at IdeOne.com.

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



About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.



来源:https://stackoverflow.com/questions/28297045/gregoriancalendar-class-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!