2012-11-28 19 views
0

如何將紐約時區時間轉換爲「亞洲/加爾各答」日期和時間?如何將紐約時區轉換爲亞洲時區

我的輸入字符串:

11/28/2012 8:59am 

我的代碼:

String dtStart = "11/28/2012 8:59am"; 
     SimpleDateFormat format = new SimpleDateFormat("mm/dd/yyyy hh:mma"); 
     format.setTimeZone(TimeZone.getTimeZone("America/New_York")); 
     try { 
      Date date = format.parse(dtStart); 
      Log.i("clock", date.toString()); 
      System.out.println(date); 


      Calendar cal = Calendar.getInstance(); 
      cal.setTimeInMillis(date.getTime()); 
      cal.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata")); 
      int chour = cal.get(Calendar.HOUR_OF_DAY); 
      int cminute = cal.get(Calendar.MINUTE); 
      int dd = cal.get(Calendar.DAY_OF_MONTH); 
      int mm =cal.get(Calendar.MONTH); 
      int yy =cal.get(Calendar.YEAR); 

      String mode="AM"; 

      if(chour>12) 
      { 
       chour=chour-12; 
       mode="PM"; 
      } 

      String mytime=Integer.toString(chour)+":"+Integer.toString(cminute)+" "+mode; 
      String mydate=Integer.toString(dd)+"/"+Integer.toString(mm)+"/"+Integer.toString(yy); 

      Log.i("clock", mytime); 
      Log.i("clock", mydate); 

     } catch (ParseException e) { 
      e.printStackTrace(); 
     } catch (java.text.ParseException e) { 
      e.printStackTrace(); 
     } 

我的輸出日誌:

Sat Jan 28 19:29:00 GMT+05:30 2012 
7:29 PM 
28/0/2012 

這裏我的時間是正確的,但日期是錯誤的。我預計28/11/2012.我無法追蹤我做錯了什麼地方?

+0

亞洲時間不應該在未來的一天左右從紐約? – kabuto178

回答

4

問題出在您輸入的格式:"mm/dd/yyyy hh:mma"。我相信你想這樣做:"MM/dd/yyyy hh:mma"

+0

這也不起作用 – Ramprasad

+1

我不知道爲什麼有**添加..重點是,你已經使用小寫「米」,其中涉及到分鐘,而不是大寫M與月有關 – iBecar

+0

修改「mm」到「MM」作品精細 – Ramprasad

相關問題