Johnny Cheuk Johnny Cheuk - 1 year ago 46
Android Question

How do I create a date field in sqlite and define the date format for it?

I want to store a date field into my database. In order to do that, I think I might need a format to store inside the database.

How can I set the format for that date field? And in the java code part, the variable I should used, is it a Integer? Or just a String?

Answer Source

you can use DATE field as varChar i.e in String format.

ex : add this to your table for date COLUMN_varDisplayDate + " Text ,"

use this method to format date as per your requirements,

public static String getDateFormat(String date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        String convertedDate = "";
        try {
            Date parseDate = dateFormat.parse(date);
            SimpleDateFormat fmtOut = new SimpleDateFormat("yyyy-MM-dd");
            convertedDate = fmtOut.format(parseDate);
        } catch (ParseException e) {
        return convertedDate;