Android Question Parse a SQLite DB String value to Date

dieterp

Active Member
Licensed User
Longtime User
I have a 'DatePlayed' field stored in a SQLite database that is saved as a String type. I am retrieving this field through a query in order to update another field called 'SQLDatePlayed' that stores the same date value but as a Date type (With format 2013-08-28). I am using the code below to try and parse the string field, but am getting the error 'java.text.ParseException: Unparseable date: "28 Aug 2013"' (at offset 2).
Any suggestions?

Dim TheDate As String
TheDate = Cursor.GetString("DatePlayed")
DateTime.DateFormat = "yyyy-MM-dd"
Dim t As Long = DateTime.DateParse(TheDate) 'Error occurs here
 

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Have you verified in the debugger what the value of TheDate is?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Since your string date is in this format: 28 Aug 2013, you need to change your date format as shown here. See code below:
B4X:
TheDate = Cursor.GetString("DatePlayed")  '28 Aug 2013
DateTime.DateFormat= "dd MMM yyyy"
Dim t As Long = DateTime.DateParse(TheDate)
DateTime.DateFormat = "yyyy-MM-dd"
TheNewDate= DateTime.Date(t)  'will allow you to store the date as 2013-08-28 in the new SQLDatePlayed
 
Upvote 0
Top