Could anyone please help figure out this issue:
In my SQLite database I have a TEXT
field startDateTimeUTCString
, which is for storing a UTC datetime string.
I have an object called "task" which has a Date
property called startDateTimeLocal
. I want to convert the value of startDateTimeUTCString
(retrieved from database) into Date
type and then convert it to local timezone value, and store it into task.startDateTimeLocal
. I pass this Date
value around my activity for processing.
After processing I want to convert the updated task.startDateTimeLocal
to UTC time first, then convert into a string value, then save the string value into database field startDateTimeUTCString
.
I have read through and tried so many implementation of using SimpleDateFormat
and DateFormat
, kept getting parse errors all the way.
Any help will be greatly appreciated.
Thanks in advance!
The property in SQLite is "startDateTimeUTC TEXT NOT NULL DEFAULT" The corresponding property in my object is "Date startDateTimeUTC": // getting StartDateTimeUTC public Date getStartDateTimeUTC(){ return this._startDateTimeUTC; }
// setting StartDateTimeUTC
public void setStartDateTimeUTC(Date startDateTimeUTC){
this._startDateTimeUTC = startDateTimeUTC;
}
On my AddNewItemActivity, I have a set of static integers: startYear, startMonth, startDay, startHour, startMinute, and a static Calendar startDateCalendar which represents the local timezone datetime value. I use a DatePickerDialogFragment and a TimePickerDiaglogFragment to set their values and set startDateCalendar to take these integer values:
startDateCalendar.set(startYear, startMonth, startDay);
startDateCalendar.set(Calendar.HOUR_OF_DAY, startHour);
startDateCalendar.set(Calendar.MINUTE, startMinute);
I then convert startDateCalendar into a UTC string and insert into 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 my DAO code GetAll() method I convert the value from String into Date type:
String startDateTimeUTCString = cursor.getString(5);
SimpleDateFormat utcFormat = new SimpleDateFormat();
Date startDateTimeUTC = utcFormat.parse(startDateTimeUTCString , new ParsePosition(0));
In my list view adapter I convert the UTC datetime value into local timezone string and display it int he list:
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.setTimeZone(TimeZone.getDefault());
String localString= sdf.format(startDateTimeUTC);
I tried various formats when declaring SimpleDateFormat no luck at all...
now I took out all the formats in the declaration codes, all server errors were gone. However I found out that the the conversion from local to UTC failed during insert, i.e. the string saved into SQLite database is not UAT......
=================================
I figured out how to convert local datetime to utc string for saving to SQLite:
sourceCalendar.setTimeZone(TimeZone.getTimeZone("UTC"));
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String outputDateString = sdf.format(sourceCalendar.getTime());
Now I am stuck on converting UTC date to local string for display purpose.....