2013-02-11 69 views
0

在Android中,我使用SqlLiteMyDate行像DATETIME DEFAULT CURRENT_TIMESTAMP。但是当我读日期时,我得到一个小时的日期,因为我在我的android环境中使用了GMT + 1从SqlLite Android的日期取决于GMT

MyDate: 12:03 
Android date: 13:03 

SqlLite读取时我如何才能将其转换为GMT + X,取决于Android的环境?

编辑:

代码:

 Cursor cursor = logsDao.getAll(LogsDao.KEY_ID + " DESC", "100"); 
      String[] columns = new String[] { LogsDao.KEY_CONTENT, LogsDao.KEY_COMMENT, LogsDao.KEY_CREATED }; 
      int[] to = new int[] { R.id.logContext, R.id.logComment, R.id.logCreated }; 
      SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, 
      R.layout.log_entry, cursor, columns, to); 
      setListAdapter(mAdapter); 

回答

1

我想,首先应该存储您检索的数据的时间戳与UCT时区,然后与mobilephone的一个根据更新当前时区。

String s = "2013-02-11 12:03:00"; 
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
df.setTimeZone(TimeZone.getTimeZone("UCT")); 
Date timestamp = null; 
try { 
    timestamp = df.parse(s); 
    df.setTimeZone(TimeZone.getDefault()); 
} catch (ParseException e) { 
    e.printStackTrace(); 
} 

您需要implement a custom cursorAdapter和方法bindView manipuulate您的日期

@Override 
    public void bindView(View v, Context context, Cursor c) { 
     String date; 
     date= c.getString(c.getColumnIndex(LogsDao.KEY_CREATED)); 

     //implement change of timezone 

     TextView date_text = (TextView) v.findViewById(R.id.date); 
     if (date_text != null) { 
      date_text.setText(date); 
     } 
    } 
+0

谢谢。我更新我的问题,因为我不知道如何在SimpleCursorAdapter – senzacionale 2013-02-11 13:23:11

+0

中使用此代码,您需要实现自定义cursorAdapter http://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/ – StarsSky 2013-02-11 13:34:06