Question

I am using the code below to access browser's history in Title, Url, Date and Time format; Title and Url works fine but while accessing Date and time it gives a long integer value. Can anyone suggest what changes I need to make in order to properly get the date and time?

String sel = Browser.BookmarkColumns.BOOKMARK + " = 0";
Cursor mCur = managedQuery(Browser.BOOKMARKS_URI, Browser.HISTORY_PROJECTION, sel, null, null);
j = mCur.getCount();
String[] mTitles = new String[j];
String[] murls = new String[j];
long[] date=new long[j];
mCur.moveToFirst();
i=j-1;

if (mCur.moveToFirst() && mCur.getCount() > 0) {
  int titleIdx = mCur.getColumnIndex(Browser.BookmarkColumns.TITLE);
  int urlIdx = mCur.getColumnIndex(Browser.BookmarkColumns.URL);
  int dateIdx=mCur.getColumnIndex(Browser.BookmarkColumns.DATE);

  while (mCur.isAfterLast() == false ) {
    mTitles[i]=mCur.getString(titleIdx);
    murls[i]=mCur.getString(urlIdx); 
    date[i]=mCur.getLong(dateIdx);
    i--;
    mCur.moveToNext();
  }
}
Was it helpful?

Solution

// using Calendar class
Calendar ci = Calendar.getInstance();

String CiDateTime = "" + ci.get(Calendar.YEAR) + "-" + 
    (ci.get(Calendar.MONTH) + 1) + "-" +
    ci.get(Calendar.DAY_OF_MONTH) + " " +
    ci.get(Calendar.HOUR) + ":" +
    ci.get(Calendar.MINUTE) +  ":" +
    ci.get(Calendar.SECOND);

and to get it in a formate you can try this.

 SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");

otherwise you can use Time class , it's a faster implementation of Calendar class http://developer.android.com/reference/android/text/format/Time.html

OTHER TIPS

The Date should be a long, since it is the number of milliseconds since January 1, 1970. An integer would be too short.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top