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

后端 未结 5 1095
醉梦人生
醉梦人生 2021-01-03 03:38

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 wha

相关标签:
5条回答
  • 2021-01-03 03:56

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

    Here is the API documentation for the same.

    0 讨论(0)
  • 2021-01-03 03:56

    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

    0 讨论(0)
  • 2021-01-03 04:13

    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.

    0 讨论(0)
  • 2021-01-03 04:15

    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

    0 讨论(0)
  • 2021-01-03 04:18

    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

    0 讨论(0)
提交回复
热议问题