android sqlite save and retrieve utc and convert to local -


could please figure out issue:

in sqlite database have text field startdatetimeutcstring, storing utc datetime string.

i have object called "task" has date property called startdatetimelocal. want convert value of startdatetimeutcstring (retrieved database) date type , convert local timezone value, , store task.startdatetimelocal. pass date value around activity processing.

after processing want convert updated task.startdatetimelocal utc time first, convert string value, save string value database field startdatetimeutcstring.

i have read through , tried many implementation of using simpledateformat , dateformat, kept getting parse errors way.

any appreciated.

thanks in advance!

the property in sqlite "startdatetimeutc text not null default" corresponding property in object "date startdatetimeutc": // getting startdatetimeutc public date getstartdatetimeutc(){ return this._startdatetimeutc; }

// setting startdatetimeutc public void setstartdatetimeutc(date startdatetimeutc){     this._startdatetimeutc = startdatetimeutc; } 

on addnewitemactivity, have set of static integers: startyear, startmonth, startday, starthour, startminute, , static calendar startdatecalendar represents local timezone datetime value. use datepickerdialogfragment , timepickerdiaglogfragment set values , set startdatecalendar take these integer values:

startdatecalendar.set(startyear, startmonth, startday); startdatecalendar.set(calendar.hour_of_day, starthour); startdatecalendar.set(calendar.minute, startminute); 

i convert startdatecalendar utc string , insert sqlite database:

simpledateformat sdf = new simpledateformat(); sdf.settimezone(timezone.gettimezone("utc")); string utcstring = sdf.format(sourcecalendar.gettime());  contentvalues contentvalues = new contentvalues();       ..... contentvalues contentvalues = new contentvalues(); contentvalues.put("startdatetimeutc", utcstring ); db.insert("task", null, contentvalues); 

in dao code getall() method convert value string date type:

string startdatetimeutcstring = cursor.getstring(5); simpledateformat utcformat = new simpledateformat();     date startdatetimeutc = utcformat.parse(startdatetimeutcstring , new parseposition(0)); 

in list view adapter convert utc datetime value local timezone string , display int list:

simpledateformat sdf = new simpledateformat(); sdf.settimezone(timezone.getdefault()); string localstring= sdf.format(startdatetimeutc); 

i tried various formats when declaring simpledateformat no luck @ all...

now took out formats in declaration codes, server errors gone. found out the conversion local utc failed during insert, i.e. string saved sqlite database not uat......

=================================

i figured out how convert local datetime utc string saving sqlite:

sourcecalendar.settimezone(timezone.gettimezone("utc")); simpledateformat sdf = new simpledateformat(); sdf.settimezone(timezone.gettimezone("utc")); string outputdatestring = sdf.format(sourcecalendar.gettime()); 

now stuck on converting utc date local string display purpose.....

this give utc system time milliseconds android.

calendar c = calendar.getinstance(); int utcoffset = c.get(calendar.zone_offset) + c.get(calendar.dst_offset);   long utcmilliseconds = c.gettimeinmillis() + utcoffset; 

Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -