Ritesh Kaushik Ritesh Kaushik - 2 months ago 18
Java Question

Time zone in Java

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

Answer

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.

Comments