2016-07-22 92 views
1

美好的一天。我正在建立一个聊天应用程序。为了达到目的,我决定把信息的日期发送出去。没有在不同国家的日期必须显示不同的信息。例如我们带两个不同的国家,假设它们之间的差别是2小时.CountryX和CountryY。用户从CountryX发送消息,时间可以说是15:00。我将这个保存在服务器上,具有用户发送的确切时区时间,正好15 :00 as CountryX.Second用户从CountryX接收2小时内CountryY的消息,所以基本上我必须在CountryY中显示的时间必须是17:00。这是问题。我如何将已经接收的时间与已知的时区到当地为了显示正确?我做了很多谷歌,但所有我想出了,解决方案,你只需要得到一个确切的国家的时间,而不是转换CountryX发送T ime到CountryY当地时间,以便正确显示在CountryY.Please你能提供帮助吗?非常感谢你事先。将接收的日期时间转换为本地时区时间Android

+0

退房lib目录下:PrettyTime它适合聊天和当地时区 –

+0

好会chec现在看看它会给出 –

+0

似乎我找不到所需的方法 –

回答

-1

给大家,因为this.I的苦难已经结束了我自己的逻辑创建我自己的类,它完美的作品,并作为额外的奖励一些方便的方法与时间称为

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(); 
    } 
} 
相关问题