Srijani Ghosh
Srijani Ghosh

Reputation: 4216

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

Upvotes: 5

Views: 533

Answers (3)

Basil Bourque
Basil Bourque

Reputation: 338211

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]


Table of date-time types in Java, both modern and legacy


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?

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.

Upvotes: 1

Dawnkeeper
Dawnkeeper

Reputation: 2877

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

Upvotes: 3

Thilo
Thilo

Reputation: 262474

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.

Upvotes: 4

Related Questions