Set Time Zone in Android App on the basis of server response

631 Views Asked by At

I am doing some work which scenario is , I am receiving date and time from server as a data field. i.e 2016-12-20T16:22:00+05:00. I have an spinner in which

*

  • (+12:00 to -12:00) values by each hour

* Now, i need to transform this date & time with respect to spinner value. i.e if spinner has selected +12:00 then it will like this 2016-12-21T04:22:00+12:00. Please suggest solutions. Thank you :)

2

There are 2 best solutions below

4
On BEST ANSWER

Try using Java String Functions like below

String time="2016-12-20T16:22:00+05:00";
String[] s =time.split("'+'");
String time1=s[0];


    String value=value from spinner;
    SimpleDateFormat readDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    readDate.setTimeZone(TimeZone.getTimeZone("GMT")); // missing line
    Date date = null;
    try {
        date = readDate.parse(time1);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    SimpleDateFormat writeDate = new SimpleDateFormat("dd.MM.yyyy, HH.mm.ss");
    writeDate.setTimeZone(TimeZone.getTimeZone("GMT+"+value));
//final time string    
String s1 = writeDate.format(date);
0
On

Try this method

public String convertTimeZone(String date, String timeZone) throws ParseException {
    StringBuffer buffer = new StringBuffer(date);
    date = buffer.reverse().toString().replaceFirst(":","");
    date = new StringBuffer(date).reverse().toString();
    SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    inFormat.setTimeZone(TimeZone.getTimeZone("GMT" + timeZone));
    Date toConvert = inFormat.parse(date);
    date = inFormat.format(toConvert);
    return new StringBuffer(date).insert(date.length()-2, ":").toString();
}