5

I have an issue of converting selected hours and minutes to different time zones of countries. Supposing if i select 10 am in India then i want to know at 10 am in india what will be the time in USA/New york and Tokyo.and Vice versa.

Any help is appreciable... Thank you

Richa
  • 3,157
  • 1
  • 20
  • 25
  • i know my problem is not to convert current time, my problem is in converting selected time – Richa Feb 02 '12 at 08:37
  • i m trying for 2 days bt i m not getting result – Richa Feb 02 '12 at 08:45
  • Can I know how you solve your Issue. I am getting an ! hour difference in USA / newyork devices. But if we change the time zone to Newyourk time zone its working. Only devices in newyork in facing a ! hour difference for me. Can I know how you solve this Issue. – Nivedh Oct 11 '16 at 03:19

5 Answers5

3

please find the sollution below :

    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mma");
    TimeZone timezone = TimeZone.getDefault();
    TimeZone utcTimeZone = TimeZone.getTimeZone("UTC");
    Date d = new Date();
    sdf.setTimeZone(timezone);
    String strtime = sdf.format(d);
    Log.e("str time gmt  ",strtime);
    sdf.setTimeZone(utcTimeZone);
     strtime = sdf.format(d);
    Log.e("str time utc ",strtime);

i think this will solve your problem

Ramesh Solanki
  • 2,966
  • 4
  • 27
  • 43
  • im getting utc time as per ur ans and not the time of desired zone,from selected hour and min – Richa Feb 02 '12 at 07:42
2

You can probably use Joda Time - Java date and time API. 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"));

For getting more idea you can refer Changing TimeZone

Lalit Poptani
  • 66,608
  • 21
  • 157
  • 241
1

you can also get it using , Here no external API is needed

DateFormat format = new SimpleDateFormat("MMMMM d, yyyy, h:mm a");
TimeZone utc = TimeZone.getTimeZone("America/New_York");
System.out.println(utc.getID());
GregorianCalendar gc = new GregorianCalendar(utc);
Date now = gc.getTime();
System.out.println(format.format(now));

you can see more time zone on this Link

Output

America/New_York

December 29, 2012, 11:04 AM

If you don't know city name then you can also use it by Zone name as follow

DateFormat format = new SimpleDateFormat("MMMMM d, yyyy, h:mm a");
TimeZone cst = TimeZone.getTimeZone("US/Eastern");
System.out.println(cst.getID());
GregorianCalendar gc = new GregorianCalendar(cst);
Date now = gc.getTime();
format.setTimeZone(cst);
System.out.println(format.format(now))

Output

US/Eastern

December 29, 2012, 12:38 AM

Ronak Mehta
  • 6,010
  • 5
  • 41
  • 68
1

Try using Joda-Time library check the org.joda.time.DateTimeZone class

Here is the API documentation for the same.

Seshu Vinay
  • 13,292
  • 9
  • 58
  • 106
0

Not really sure about the solution I'm going to provide but I think you can try it. GMT (Greenwich Mean Time) is a standard. I think you can keep it as a base and calculate the desired time. GMT standard is easily available too.

For example: While installing an OS like Windows XP or Windows 7, we select the time from a drop down menu. My point is, keeping this as the base, you can find the difference between the time zones in NY-US and Tokyo-Japan or vice versa as you desire it.

Hope this helps.

Ghost
  • 3,966
  • 1
  • 24
  • 35