How can i know time in a specific country if i am in a differenet country?

泄露秘密 提交于 2019-12-20 03:54:31

问题


I am trying to do it this way 1. Get my time current time and time zone. 2. I know time zone of that specific country from Google. 3. Calculate difference in time zones. 4. Subtract this difference from current time. This will give me time in other country. I am stuck at step 3 and 4. I get current time like this

Date d = new Date();
CharSequence s = DateFormat.format("hh:mm:ss", d.getTime());

and time zone like this

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"),
            Locale.getDefault());
    Date currentLocalTime = calendar.getTime();
    SimpleDateFormat date = new SimpleDateFormat("Z");
    String localTime = date.format(currentLocalTime);

let time zone of specific country be represented by string x.Now how do i get localTime - x and then currentTime - (localTime - x).


回答1:


You can use Joda-Time library,

Joda-Time User Guide

http://www.joda.org/joda-time/

How can i know time in a specific country if i am in a differenet country?

http://www.joda.org/joda-time/apidocs/index.html

You can get the DateTimeZone depending on the Canonical ID defined in the Joda Time,

DateTimeZone zone = DateTimeZone.forID("Asia/Kolkata");

Joda Time has a complete list of Canonical ID from where you can get TimeZone depending on the Canonical ID.

So, if you want to get the local time in New York at this very moment, you would do the following

 // get current moment in default time zone
    DateTime dt = new DateTime();
    // translate to New York local time
    DateTime dtNewYork = dt.withZone(DateTimeZone.forID("America/New_York"));

here it is explained very perfectly which is my reference to your question

Get time of different Time zones on selection of time from time picker

and you can also use this link without getting use of api

http://tutorials.jenkov.com/java-date-time/java-util-timezone.html

other references

How to get time with respect to another timezone in android

Date and time conversion to some other Timezone in java

How to convert date time from one time zone to another time zone




回答2:


x is the time zone id, there are a link of a list of them. Only need to feed the gettimeZone("with the time zone id") with the time zone of the city you want to know the time (I'm saying city because there are country with a lot of time zones like US or Russia).

TimeZone tz = TimeZone.getTimeZone("x")

http://garygregory.wordpress.com/2013/06/18/what-are-the-java-timezone-ids/



来源:https://stackoverflow.com/questions/24093414/how-can-i-know-time-in-a-specific-country-if-i-am-in-a-differenet-country

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