2

I'm working on a project and i want to convert server time to local time using GMT. example my server is in Michigan in US and i'm living in Paris (France) and server time is 10:52 and i want to be converted to my local time 16:52 i use codes below and it does not seem to work. This convert time using GMT +12 instead of +6 and i don't know why help me please!!!!!!

public String GettingHeure(Context c, String date){
    SimpleDateFormat datechanged = new SimpleDateFormat("HH:MM", Locale.getDefault());
    Date dates = null;
    String heureChanged = null;
    try {
        SimpleDateFormat formatter = new SimpleDateFormat("hh:mm",Locale.getDefault());
        dates = formatter.parse(date);

        TimeZone tz = TimeZone.getDefault();
        formatter.setTimeZone(TimeZone.getTimeZone(tz.getID()));

        System.out.println("local time : " + dates);
        System.out.println("time in GMT : " + formatter.format(dates));
        //Toast.makeText(c.getApplicationContext(), "String : "+ date+" "+formatter.format(dates), Toast.LENGTH_LONG).show();
        heureChanged = formatter.format(dates);
        SimpleDateFormat formatters = new SimpleDateFormat("hh:mm");

        Date datess = formatters.parse(heureChanged);

        System.out.println("local time : " + datess);
        System.out.println("time in GMT : " + formatters.format(datess));
        heureChanged = formatter.format(datess);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return heureChanged;
Henock Bongi
  • 255
  • 5
  • 16
  • 2
    It would be a lot better if you got the time from the server in a more universally understood format, like number of seconds or milliseconds since the "epoch" (Jan 1, 1970). You wouldn't have to parse strings in that case. – Doug Stevenson Feb 13 '16 at 18:11
  • Thanks i'm working on it! – Henock Bongi Feb 15 '16 at 11:05

1 Answers1

2

If i understood your problem correctly, you want to convert time between time zones. Here is the code to do that.

    public void convertDate(Date d) {  

    //You are getting server date as argument, parse your server response and then pass date to this method

    SimpleDateFormat sdfAmerica = new SimpleDateFormat("hh:mm:ss");

    String actualTime = sdfAmerica.format(d);
    //Changed timezone
    TimeZone tzInAmerica = TimeZone.getTimeZone("CST");
    sdfAmerica.setTimeZone(tzInAmerica);

    String convertedTime = sdfAmerica.format(d);

    System.out.println("actual : " + actualTime + "  converted " + ConvertedTime);

    return convertedTime;
    }

Hope this will help.

sandip
  • 81
  • 5
  • Thanks i try to get Date Object from server then send it to android device then setTimeZone to it – Henock Bongi Feb 15 '16 at 12:04
  • I have update the code where you would be using the above function on android side and passing the date or time as argument which you are getting from server – sandip Feb 16 '16 at 09:31
  • please answer this too: http://stackoverflow.com/questions/38009126/how-to-show-the-time-and-date-saved-in-my-database-to-the-user-according-to-thei Please... – Hammad Nasir Jun 24 '16 at 13:32