2017-02-08 3 views
-1

私はアンドロイドデベロッパーが初めてです。 GMTを地元のモバイル時間に変換します。このコードではam/pmの問題があります。 6時以降の夕方の時間。私は転換期に入っている。申し訳ありません。私の英語は です。アドバイスありがとうございました。GMTをアンドロイドでローカルタイムフォーマットに変換する

public String formatDate(String s) 
    { 
     String outputText=null; 
     try { 
//   Tue May 21 14:32:00 GMT 2012 
      String inputText =s; 
      SimpleDateFormat inputFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm 'GMT'", Locale.US); 
      inputFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC")); 


      if(android.text.format.DateFormat.is24HourFormat(CalloutAvalibality.this)) 
      { 

       SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd,yy HH:mm"); 
       // Adjust locale and zone appropriately 
       Date date = inputFormat.parse(inputText); 


       outputText= outputFormat.format(date)+" "+"Hrs"; 
       System.out.println(outputText); 

      } 
      else 
      { 
       SimpleDateFormat outputFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm a"); 

       // Adjust locale and zone appropriately 
       Date date = inputFormat.parse(inputText); 
       outputText= outputFormat.format(date); 

//    outputText=outputText.replace("AM","am"); 
//    outputText=outputText.replace("PM","pm"); 
       System.out.println(outputText); 
      } 


     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return outputText; 
    } 

答えて

0

datefomater.format()は、フォーマッタオブジェクトで最初に設定したタイムゾーンに変換される文字列を返します。

datefomater.parse()Dateオブジェクトは、デフォルトのタイムゾーンに

TimeZone timeZone = TimeZone.getTimeZone("America/Chicago"); 
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
formatter.setTimeZone(timeZone); 

String result = formatter.format(YOUR_DATE_OBJECT); 
を設定しますあなたにローカルタイムゾーン

ですDateオブジェクトを返します。

関連する問題