0
votes

I insert date in sqlite database in a supported format (yyyy-mm-dd). Now I would like the user at the time of the query to display the date format according to country (Italy dd-mm-yyyy, America yyyy-mm-dd etc ...)

How do I? Thanks

3

3 Answers

5
votes

SQLite does not have a dedicated date and time data type. If you insert something like "01-01-2013" it will be stored like that, as a string, making comparisons, sorting and queries difficult and slow because you need to run conversions on that using SQLite date functions.

You should store UNIX timestamps instead. That requires the date column to be of type INTEGER. Timestamps can be quickly handled, sorted and selected and you can represent them in any date format you wish by using Java's Calendar and DateFormat classes, for example. You can retrieve an appropriate format for the user's default locale through factory methods.

On top of that there's Android's dedicated DateUtils class that provides various functions for creating date-time and time range strings in the user's locale.

2
votes

You can also use the SQLite date and time formatter, something like:

SELECT strftime( '%d-%m-%Y', birthday) as birthday FROM people
1
votes

Try the following code,

String date = "2013-11-15"; // Retrived date in your specified format from sqlite
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date res = null;
    try {
        d = (Date)sdf.parse(date);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    Calendar c = Calendar.getInstance();
    c.setTime(d);

    String day = c.get(Calendar.DAY_OF_MONTH);
    String month = c.get(Calendar.MONTH);
    String year = c.get(Calendar.YEAR); 
//You can use these day, month and year string values to view in any format

Hope this will help you. Thank you.