2017-02-08 1 views
-1

나는 안드로이드 개발자를 처음으로 사용하고 있습니다. GMT를 현지 모바일 시간으로 변환합니다. 이 코드에서 오전/오후 문제가 있습니다. 6'o 시계 저녁 시간 후. 나는 전환에 빠졌어. 영어로 죄송합니다. 사전 도움에 감사드립니다.GMT를 android에서 현지 시간 형식으로 변환

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); 
를 기본값으로 설정합니다