Reputation: 710
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
*
* 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 :)
Upvotes: 0
Views: 616
Reputation: 165
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();
}
Upvotes: 0
Reputation: 2437
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);
Upvotes: 1