Search code examples
androidsqlitedatetimestrftime

android sqlite strftime long month name


With this query, I get from the database, the month in numeric format. can I get the month in long format? 'MMMM'

private List<String> ottieniMesi(){
    List<String> result1 = new LinkedList<String>();

    SQLiteDatabase db = new DatabaseHelper(getActivity()).getReadableDatabase();

    String sql = "SELECT DISTINCT strftime('%m',"+Table.DATE+") FROM "+Table.TABLE_NAME;
    Cursor c = db.rawQuery(sql, null);

    while (c.moveToNext()){
        result1.add(c.getString(0));

    }
    db.close();
    return result1;
}

Solution

  • No. sqlite date and time functions do not support such conversion.

    Consider doing the conversion in your application code instead.

    For example:

    int monthNumber = ...; // the value from query
    monthNumber--; // convert to 0-based for Calendar
    Calendar c = Calendar.getInstance();
    c.set(Calendar.MONTH, monthNumber);
    String monthName = c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());