0

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 :)

Syed Hamza Hassan
  • 710
  • 1
  • 11
  • 24

2 Answers2

1

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);
Syed Hamza Hassan
  • 710
  • 1
  • 11
  • 24
Rajesh.k
  • 2,327
  • 1
  • 16
  • 19
  • Dear Rajesh, your solution is partially right but sets only GMT time, i need to change date as well as if addition of time crossed the number 24. then i need to take care about change of year and month as well. i.e if i got time from server(2016-12-31T22:22:22+05:00) and my select GMT is +12:00 then it would be like this 2017-1-1T05:22:22+12:00 Please note difference date as well – Syed Hamza Hassan Dec 20 '16 at 11:51
  • I understand what u need.check out the new code above! – Rajesh.k Dec 20 '16 at 12:30
  • your solution is right :) Bit error correction use '+' inside commas and use String s1. – Syed Hamza Hassan Dec 20 '16 at 16:48
  • Ok..Mark it as Right Answer so that it will help Other peoples. – Rajesh.k Dec 21 '16 at 05:11
0

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();
}
Shubham
  • 165
  • 2
  • 11