0

I want code for get DATETIME Value from given sqlite table and substract it from current DATETIME and get seconds or milliseconds.

db.execSQL("INSERT INTO Schedule VALUES('1','SampleText','2017-09-26 21:00:00');");
  • 1
    And "it doesn't work" means exactly what? Btw. your expression will give milliseconds, not seconds. – Henry Sep 26 '17 at 17:51
  • Can you give an example of `start_time`? How does the string value look like? – Henry Sep 26 '17 at 17:55
  • start_time value : "yyyy-MM-dd HH:mm:ss" and type is DATETIME in sqlite – Darshan Patel Sep 26 '17 at 17:56
  • Hmm, you mean that literally? Should not there be digits in it? – Henry Sep 26 '17 at 17:57
  • No its just format it will contain real values like "2017-09-26 11:29:00" – Darshan Patel Sep 26 '17 at 17:59
  • You think this is the format or did you check that by writing out a value or checking with the debugger? – Henry Sep 26 '17 at 18:00
  • Need actual values and substract it.I have DATETIME stored in String, but for subtract it will need in DATE TYPE. – Darshan Patel Sep 26 '17 at 18:02
  • 1- To get the current date, you could just do `Date d1 = new Date()`, there's no point in formatting and parsing again if you can create a `Date` directly. 2- As the others already asked, what exactly is not working? Please **[edit]** the question and put the inputs and also expected outputs vs wrong output you're getting. Just saying *"it doesn't work"* is too vague and we can't guess what's wrong unless you tell us. Please read the [ask] page and how to provide a [mcve]. –  Sep 26 '17 at 18:40
  • For get current date i can use `Date d1 = new Date()` but in other value i have sqlite DATETIME Value how can i store it in Date variable and Substract with current date ? – Darshan Patel Sep 26 '17 at 18:56
  • Once again: what's the error you're getting? I've just ran your code (with `start_time` equals to `"2017-09-26 11:29:00"`) and it works - I could get `d1` and `d2` variables (the 2 `Date` variables are correctly created). In what point are you stuck? In your code, the variable `diff` contains the difference between the 2 dates, in milliseconds. What else do you need? –  Sep 26 '17 at 19:24
  • `String start_time = resultSet.getString(2);` – Darshan Patel Sep 27 '17 at 05:33
  • In short what i want is that `db.execSQL("INSERT INTO Schedule VALUES('1','SampleText','2017-09-26 21:00:00');");` fetch data from database and substract datetime from current datetime. – Darshan Patel Sep 27 '17 at 05:43
  • Hope this helps you :- https://stackoverflow.com/questions/6495964/subtracting-two-datetime-variable – InsaneCat Sep 27 '17 at 05:56
  • No it wil not work because thats for c# language.I want for android. – Darshan Patel Sep 27 '17 at 06:09
  • *"Unhandled Exception Error"* occurs because the `parse` method throws a `ParseException`. Just surround your code with a `try`/`catch` block and you're done. –  Sep 27 '17 at 13:10
  • I have tried `try / catch` but it will returns the catch block, i want to execute that parse method. – Darshan Patel Sep 28 '17 at 09:31
  • 1
    This is worse than pulling teeth. When you get a parse exception the string value DOES NOT HAVE THE FORMAT YOU THINK. You are struggling with this really basic problem for two days now. Why do you not just check what you get back from the DB? – Henry Sep 29 '17 at 04:38
  • *but it will returns the catch block* - I don't get what you mean. Could you please [edit] the question and add the parsing code with the try/catch block? (wondering why you removed the code from the question, it's the most important part). –  Sep 29 '17 at 18:29
  • @Darshan Patel this is simple look at this post [enter link description here](https://stackoverflow.com/questions/21285161/android-difference-between-two-dates) – Vector Oct 07 '17 at 22:45

0 Answers0