0

I have a date picker in my app returning the date as String in the form (yyyy-mm-dd). The problem I have is how to convert it from String to Integer or any other format to insert it into SQLite, that I can query it later.

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 String formattedDate = sdf.format(c.getTime()); 
Wolf
  • 9,679
  • 7
  • 62
  • 108
Karam Melad
  • 205
  • 2
  • 9
  • 1
    Possible duplicate of [Best way to work with dates in Android SQLite](http://stackoverflow.com/questions/7363112/best-way-to-work-with-dates-in-android-sqlite) – Stephan Mar 13 '16 at 11:18

3 Answers3

0

you have to convert the date string to milliseconds then you can do whatever you want to do with it from there, look at this stackoverflow answer converting a date string into milliseconds in java

Community
  • 1
  • 1
Aliyu Abdullahi
  • 129
  • 1
  • 4
0

Inserting the date to sqlite could be done (even if it might not be good practice) as a string too. Sqlite is not too strict about types. Nevertheless, you could in theory convert the date to millis. Something like

 Date d = format.parse(<<Your Date String>>)

(see http://www.java2s.com/Tutorial/Java/0040__Data-Type/SimpleDateFormat.htm) and then you insert into DB the

d.getTime()

For an explanation see http://www.tutorialspoint.com/java/util/date_gettime.htm.

Christian
  • 303
  • 2
  • 15
0

1.2 Date and Time Datatype

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS"). REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar. INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC. Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.

See SQLite documentation

Date And Time Functions DateTime SQLite functions

dieter_h
  • 2,707
  • 1
  • 13
  • 19