Johnny Cheuk Johnny Cheuk - 5 months ago 14
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

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) {
            e.printStackTrace();
        }
        return convertedDate;
    }