2015-11-02 20 views
0

我收到了以下從後端動態設置時區,並在印度的標準格式

[ 
    { 
     "time": "4:40pm", 
     "country": "Australia" 
    }, 
    { 
     "time": "3:30pm", 
     "country": "america" 
    }, 
    { 
     "time": "6:30am", 
     "country": "mexico" 
    } 
] 

我每次都需要解析此JSON,並轉換爲IST(印度)時間這種格式的JSON數組的形式返回時間

我開始這樣,我設置的時區,但也不能這個時候IST

import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.TimeZone; 

public class Testing { 
    public static void main(String[] args) throws ParseException { 
     String time = "4:40pm"; 
     String country = "Australia"; 
     convertDate(time, country); 
    } 

    public static String convertDate(String time, String country) 
      throws ParseException { 
     SimpleDateFormat in = new SimpleDateFormat("hh:mm"); 

     if (country.equals("Australia")) { 
      in.setTimeZone(TimeZone.getTimeZone("Australia/Sydney")); 
      in.parse(time).toString(); 

     } 
     return ""; 
    } 
} 
+0

請參閱本http://stackoverflow.com/questions/6567923/timezone-conversion – Shankar

+1

什麼時區,你的事「美國」是指?美國有很多時區......也請注意,你正在調用'toString()',但總是隻返回「」...而且你忽略了方法的返回值。目前還不清楚你希望*這個代碼實現什麼... –

+0

@Preethi你可以轉換爲UTC時間,然後轉換爲IST,這將是兩步,並始終爲每個時區轉換工作 – Shankar

回答

1

你ñ轉換在那裏。

如果你要打印適應IST的hh:mm格式,您可以:

if (country.equals("Australia")) { 
    in.setTimeZone(TimeZone.getTimeZone("Australia/Sydney")); 
    SimpleDateFormat out = new SimpleDateFormat("hh:mm"); 
    out.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata")); 
    return out.format(in.parse(time)); 
} 
+1

Ick手動日期/時間格式化 - *更好*在適當的時區設置下使用新的SimpleDateFormatter。 –

+0

@JonSkeet我同意,在那裏有點過快和骯髒。 – Mena

+0

謝謝,但它給了我錯誤的時間(應該是上午11:10)價值4:40 pm在austrlaia – Pawan