0

This may be a very basic question, but i could'nt find any satisfactory answers.Hope my doubts gets clear on stackoverflow.

Q 1. Suppose i have time in a different timezone and i want to convert it to a different timezone, what is the way to do it in Java?

Q 2. Is there any way to get timezone using JavaScript?

Q 3. A timezone is just the representation of time in a particular zone, but actually every zone is at the same time,just representation wise it may be different depending on geographical conditions. - Is this understanding Correct?

possible duplicate link

Community
  • 1
  • 1
Ritesh Kaushik
  • 715
  • 2
  • 13
  • 24
  • 1
    And java != javascript – Abubakkar Feb 05 '13 at 09:22
  • 1
    Are you messing up Java with JavaScript? They are different. – Alvin Wong Feb 05 '13 at 09:22
  • Thnx guys, possible duplicate [link](http://stackoverflow.com/questions/9867254/java-timezone-why-different-timezone-give-same-value-in-millisec) – Ritesh Kaushik Feb 05 '13 at 09:24
  • 1
    A timezone is made of UTC offsets and rules to determine which of the UTC offsets is in effect at a given point in time. The common mistake is to think that timezone == UTC offset – Esailija Feb 05 '13 at 09:24
  • Answer 3) A timezone is not a representation of time - if i travel to say Australia then i am in fact travelling into the future. If i go across the timezone boundary and end up in say Hawaii then I have in fact time travelled – maloney Feb 05 '13 at 09:28
  • Java has a class for managing timezones and it's called, wait for it: TimeZone – Matei Suica Feb 05 '13 at 09:32

3 Answers3

2

Suppose i have time in a different timezone and i want to convert it to a different timezone, what is the way to do it in Java?

Create a formatter and set the timezone in there.

Internally, java.util.Date just stores milliseconds since the Epoch in the UTC timezone. When you use Date.toString() or new SimpleDateFormat() without a timezone, then the default timezone of your VM is used.

So in a sense, Java always converts to your current/default timezone (unless you happen to be in UTC).

Is there any way to get timezone using Java Script?

It depends. You can use getTimezoneOffset() but that gives you only the offset. There is no API to get the client's OSs timezone ID (like UTC, Europe/Berlin, etc.)

A timezone is just the representation of time...

Not really. See above.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • It is possible to determine the actual timezone like `Europe/Berlin`. For each timezone in the timezone database, you can assert all the rules, and if they pass, the client is in that timezone. To assert a rule, you set a date object to the rule's date and see if `.getTimezoneOffset()` matches the rule. There are simplified implementations of this available as libraries and one in [google closure](http://closure-library.googlecode.com/svn-history/r27/trunk/closure/goog/docs/closure_goog_locale_timezonedetection.js.html) – Esailija Feb 05 '13 at 09:38
  • @Esailija: Interesting approach and probably useful as long as you don't expect the result to be 100% accurate (i.e. you could get an ID that has the same offset but the client might actually be somewhere else). – Aaron Digulla Feb 05 '13 at 09:43
  • Well it doesn't detect location, but timezone (the one that's useful in programming). See my comment in the main question - if two distinct timezone IDs have exactly the same rules then it doesn't matter which one you use. And if they have different rules, then only one will be detected, provided you probe the date objects extensively enough. – Esailija Feb 05 '13 at 09:46
  • 1
    Btw, I agree it doesn't have to be 100% but you have a pretty good chance of getting a good default without having to ask the user to select their timezone explicitly. You can always provide the option to change it manually. – Esailija Feb 05 '13 at 09:51
  • Your rules should account for such oddities as daylight savings time in each locale. Good luck! – Peter Davis Dec 06 '13 at 17:35
1

Q 1. Suppose i have time in a different timezone and i want to convert it to a different timezone, what is the way to do it in Java?

The modern way is with the java.time classes.

Firstly, do much of your work in UTC. Apply a time zone only where necessary, such as presentation to a user.

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds.

Instant instant = Instant.now();

If you have only an offset-from-UTC rather than a time zone, apply a ZoneOffset to get a OffsetDateTime.

ZoneOffset offset = ZoneOffset.ofHours( -4 );
OffsetDateTime odt = instant.atOffset( offset );

A time zone is an offset-from-UTC (a specific number of hours, minutes, and seconds) plus a set of rules for handling anomalies such as Daylight Saving Time (DST). Represent a time zone with a ZoneId object. Specify a proper time zone name. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

Apply a ZoneId to get a ZonedDateTime.

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );

You can apply other time zones to either the Instant or the ZonedDateTime.

ZoneId zParis = ZoneId.of( "Europe/Paris" );
ZonedDateTime zdtParis = zdt.withZoneSameInstant( zParis );

Q 2. Is there any way to get timezone using JavaScript?

The issue of determining a time zone from a web browser has been handled countless times on Stack Overflow already.

So I'll skip this, except to say the upshot: No, not really in a reliable way; When important to know the time zone precisely you must ask the user herself.

Q 3. A timezone is just the representation of time in a particular zone, but actually every zone is at the same time,just representation wise it may be different depending on geographical conditions. - Is this understanding Correct?

No, a time zone is not a date-time moment.

A time zone adds meaningful context to a date-time in the same way that a currency designation adds meaningful context to an amount of money. A date-time without a time zone is just a rough idea of possible moments, not a precise point on the timeline. Noon at Auckland is earlier than noon in Kolkata which is earlier than noon in Paris which is earlier than noon in Montréal Québec.

You can think of it as pseudo-math statement:

Time Zone = ( Offset-from-UTC + set-of-rules-for-anomalies )

An example of an imaginary time zone:

  • An offset might be “one hour ahead of UTC”, plus
  • This set of rules: “On this date we will engage DST, on this date we will disengage DST, on this date during World War II we did shift ahead one hour, on this date after World War II we shifted back one hour, on this date our government shifted clocks forward a half-hour to make us distinct from our arch-rival neighbor country, …”.

You can apply a time zone to a point on the timeline. Like looking at art through a lens, it changes your perception but not the artifact itself. Looking at a point on the timeline through the lens of a time zone distorts the time-of-day and possibly the date into that of a particular community’s wall-clock time.

Another pseudo-math equation as a way of thinking about the class representations of a time zone and a moment on the timeline:

ZonedDateTime = Instant + ZoneId


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.

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

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

Using a JDBC driver compliant with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings nor 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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

I can answer to your second question and correct the Aaron Digulla very complete response

Is there any way to get timezone using Java Script?

try to use this library, it will return a TimeZone ID with particular limitation (menthioned in the developer's page):

https://bitbucket.org/pellepim/jstimezonedetect

Marco
  • 1,284
  • 4
  • 17
  • 41