Load Date value from cursor by column name - Android Database

Android examples for Database:Cursor Column

Description

Load Date value from cursor by column name

Demo Code


//package com.java2s;

import android.database.Cursor;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import java.util.Locale;

public class Main {
    public static Date getDate(Cursor cursor, String columnName) {
        String dateString = cursor.getString(cursor
                .getColumnIndex(columnName));
        if (dateString == null) {
            return null;
        }//from  w  ww  .  j  a va  2  s .c o  m
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
                Locale.ENGLISH);
        Date date = null;
        try {
            date = format.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
}

Related Tutorials