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

后端 未结 5 1096
醉梦人生
醉梦人生 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

    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

提交回复
热议问题