2012-02-02 132 views
5

我有一个问题,将选定的小时和分钟转换为国家的不同时区。 假设我在印度选择10点,那么我想知道在印度上午10点在美国/纽约和东京的时间是什么,反之亦然。获取不同时区的时间选择时间选择器

任何帮助是明显的... 谢谢

+0

我知道我的问题不是当前的时间转换,我的问题是在把选定的时间 – Richa 2012-02-02 08:37:39

+0

我尝试2天BT我不是获得结果 – Richa 2012-02-02 08:45:36

+0

我可以知道您是如何解决问题的。我得到了!美国/纽约设备的小时差异。但是,如果我们将时区更改为Newyourk时区的工作。只有在纽约的设备面临着!小时的差异对我来说。我可以知道你是如何解决这个问题的。 – Nivedh 2016-10-11 03:19:11

回答

0

不能确定有关解决方案,我将提供,但我认为你可以尝试一下。 GMT(格林威治标准时间)是一个标准。我认为你可以保持它作为一个基础,并计算所需的时间。 GMT标准也很容易获得。

例如:安装Windows XP或Windows 7等操作系统时,我们从下拉菜单中选择时间。我的观点是,以此为基础,你可以找到纽约和日本之间的时区差异,反之亦然。

希望这会有所帮助。

3

请在下面找到sollution:

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

我认为这将解决您的问题

+0

即时通讯的时间,而不是所需区域的时间,从选定的小时和分钟 – Richa 2012-02-02 07:42:19

2

你或许可以使用Joda Time - Java date and time API。你可以得到DateTimeZone取决于在约达时间定义的Canonical ID

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

乔达时间从那里你可以根据规范ID获取TimeZone的一个完整的list of Canonical ID

所以,如果你想在纽约当地时间在这个非常时刻,你会做以下

// 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")); 

为了得到更多的想法,你可以参考Changing TimeZone

+0

我知道我不能理解它! – Richa 2012-02-04 06:22:33

+1

你在使用这个时面临什么问题? – 2012-02-04 06:29:26

1

你也可以得到使用它,在这里没有外部API需要

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

你可以看到更多的时区在这Link

输出

美国/纽约

2012年12月29日,11:04

如果你不知道城市的名字,那么你还可以通过区域名称的使用遵循

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

输出

美国/东部

2012年12月29日,上午12时38分