2016-07-22 3 views
1

おはようございます。私はチャットアプリケーションを作成しています。メッセージの日付を記入することを決めました。異なる国で日付が異なる必要があります。 2つの異なる国を取って、それらの違いを2時間と仮定しましょう.CountryXとCountryY。ユーザーがCountryXからメッセージを送信する時間は15:00とします。正確なタイムゾーン時間をサーバーに保存します。 :00としてCountryXを送信します。2番目のユーザーはCountryXのメッセージをCountryXから2時間後に受信します。基本的にCountryYに表示する必要がある時間は17:00にする必要があります。これは問題です。正しいことを示すためにローカルに知られているタイムゾーンを使っていますか?私はグーグルで多くをしましたが、私が思いついたのは、正確な国の時刻を取得し、CountryXを変換しなかったソリューションでした私はCountryYに現地時間を正確に表示するために現地時間に対応しています。助けてもらえますか?ありがとうございます。誰もが原因this.Iの苦しみのためにConvert受信した日付時刻を現地のタイムゾーン時間に設定します。Android

+0

と便利な方法の余分なボーナスカップルと呼ばれるlibにチェックアウト:PrettyTimeをチャットと現地時間帯のスーツ –

+0

okはchecに何が表示されるかを確認します –

+0

必要な方法が見つからないようです –

答えて

-1

は私の独自のロジックで自分のクラスを作成してしまっていると、それは完璧に動作し、時間

public class Time { 
    public static String getCurrentTime() { 
     Calendar calendar = Calendar.getInstance(); 
     int year = calendar.get(Calendar.YEAR); 
     int month = calendar.get(Calendar.MONTH) + 1; 
     int day = calendar.get(Calendar.DAY_OF_MONTH); 
     int hour = calendar.get(Calendar.HOUR_OF_DAY); 
     int minute = calendar.get(Calendar.MINUTE); 
     int second = calendar.get(Calendar.SECOND); 
     String finalFormat = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second; 
     if (month < 10) { 
      String finalMonth = "0" + month; 
      finalFormat = year + "-" + finalMonth + "-" + day + " " + hour + ":" + minute + ":" + second; 
     } 

     return finalFormat; 
    } 

    public static String convertToLocalTime(String timeToConvert) { 
     SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
     sourceFormat.setTimeZone(TimeZone.getTimeZone(Constants.SERVER_TIME_ZONE));//where server time zone is the time zone of your server as defauul,E.X -America/Los_Angeles 
     Date parsed; 
     try { 
      parsed = sourceFormat.parse(timeToConvert); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
      return ""; 
     } 

     TimeZone tz = TimeZone.getDefault(); 
     SimpleDateFormat destFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 
     destFormat.setTimeZone(tz); 

     String result = destFormat.format(parsed); 
     return result; 
    } 

    public static String getTimeZone() { 
     return TimeZone.getDefault().getID(); 
    } 
} 
関連する問題