java - Localize time in Android -


i working on android application. user can send post , can see each other's post, pretty twitter. here's issue time.

when server serialized timestamp db , send response, responses string this: 2011-09-01 13:20:30+00:00" think +00:00 part utc offset.

i wondering what's approach parse string time object in local time zone? can show correctly on ui?

thanks!

simpledateformat has capability of capturing offset independently in meaningful way. want translate offset zone without explicitly specifying parser instance.

using simpledateformat

simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ssxxx"); try {     system.out.println(sdf.parse(date)); } catch (parseexception e) {     e.printstacktrace(); } 

gives,

thu sep 01 18:50:30 ist 2011 

joda time library provides alternative resolution.

datetimeformatter capable of parsing format z added pattern , parsed date in users default timezone.

datetimeformatter parser = datetimeformat.forpattern("yyyy-mm-dd hh:mm:ssz"); string date = "2011-09-01 13:20:30+00:00";  system.out.println(parser.parsedatetime(date)); system.out.println(parser.parsedatetime(date).todate()); 

gives,

2011-09-01t18:50:30.000+05:30 thu sep 01 18:50:30 ist 2011 

you can pick of date formats above, modify it, further process , use part of application.


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -