0

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.....

4

2 回答 2

1

这将为您提供 Android 的 UTC 系统时间毫秒。

Calendar c = Calendar.getInstance();
int utcOffset = c.get(Calendar.ZONE_OFFSET) + c.get(Calendar.DST_OFFSET);  
Long utcMilliseconds = c.getTimeInMillis() + utcOffset;
于 2013-05-09T18:03:51.863 回答
0

我不确定这是否是 Date 的工作方式,一旦我将字符串转换为 Date ,无论我将 SimpleDateFormat 设置为哪个时区,它都会在本地 TimeZone 中显示值。我需要担心的是在提交到数据库时将日期转换为 UTC 字符串。我不确定这对面临同样问题的其他人是否有帮助,目前我认为问题已解决。

于 2013-04-20T16:51:34.100 回答