Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to date in SQLITE

Tags:

android

sqlite

I can convert SQLITE date format to string of any format using strtime function i.e

select  strftime('%Y-%d-%m-%Y', '2012-09-13 12:44:22');

but I have String which is in dd-MM-yyyy format, How can I convert to yyyy-MM-dd format in sqlite so that I can use that in date comparison.

like image 287
Sharanabasu Angadi Avatar asked Oct 31 '25 08:10

Sharanabasu Angadi


2 Answers

Use something like this:

select datetime(substr(col, 7, 4) || '-' || substr(col, 4, 2) || '-' || substr(col, 1, 2)) from table;

where col is the column of table having the date in yyyy-MM-dd format

like image 85
Diego Torres Milano Avatar answered Nov 01 '25 23:11

Diego Torres Milano


You can try this

String date = "23-03-2013";
try {
    SimpleDateFormat format = new SimpleDateFormat(""dd-MM-yyyy");

    SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");

   date = df2.format(format.parse(str));

} catch (java.text.ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
like image 33
Nirali Avatar answered Nov 01 '25 23:11

Nirali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!