1

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).

user3714283
  • 31
  • 1
  • 7
  • possible duplicate of [Joda-Time DateTime.toDate() reverts back timezone](http://stackoverflow.com/questions/24044810/joda-time-datetime-todate-reverts-back-timezone) – Basil Bourque Jun 07 '14 at 03:51
  • java.util.Date dateIn = new java.util.Date(); DateTime dateTimeUtc = new DateTime( dateIn, DateTimeZone.UTC ); This gives me error that DateTime can't be resolved to a type. – user3714283 Jun 07 '14 at 03:58
  • Asked and answered many times on StackOverflow. Search for "Joda DateTimeZone" or "ZonedDateTime". – Basil Bourque Jun 07 '14 at 04:04
  • That code quote refers to Joda-Time, an excellent date-time library. So you need to include the jar in your project, and add `import` statements for the `org.joda` package. We use Joda-Time because the java.util.Date and .Calendar classes bundled with Java are notoriously troublesome and should be avoided. – Basil Bourque Jun 07 '14 at 04:08
  • Thanks, it is awesome. – user3714283 Jun 07 '14 at 04:24

2 Answers2

1

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/

Narksh
  • 11
  • 2
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

Community
  • 1
  • 1
Bharadwaja Bapatla
  • 3,281
  • 1
  • 12
  • 14