Android Question DateTime.DateParse

Jonathan Rigley

Member
Licensed User
Longtime User
I have my date stored in lngDate as 25/12/2014. I want to parse this to show 25th December 2014

This is the code I'm using but the program just shuts down and doesn't fully run.

Dim lngDate As Long = "25/12/2014"
DateTime.DateFormat = "dd MMM yyyy"
Msgbox(DateTime.DateParse(lngDate), "Date")

How do I change the string 25/12/2014 into 25th December 2014?

Many thanks Jonathan
 

sirjo66

Well-Known Member
Licensed User
Longtime User
The problem is in this line:
Dim lngDate As Long = "25/12/2014"
lngDate is long or string ??
You need to convert the date from string type to long type by using Parse function, then change format and change in string again

Sergio
 
Upvote 0

Jonathan Rigley

Member
Licensed User
Longtime User
Thank you Sergio,

I tried:

Dim sDate As String = "25/12/2014" 'String Date
Dim lngDate As Long = DateTime.DateParse(sDate) 'Convert String to Long with Date Parse
DateTime.DateFormat = "dd MMM yyyy" 'Format Date
Msgbox(DateTime.Date(lngDate), "Date") 'Show Date

Again the app just closes down. I might be out my depth here, could you maybe show an example of how you would do it?

Jonathan
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Dim sDate As String = "25/12/2014" 'String Date
Dim lngDate As Long = DateTime.DateParse(sDate) 'Convert String to Long with Date Parse
DateTime.DateFormat = "dd MMM yyyy" 'Format Date
Msgbox(DateTime.Date(lngDate), "Date") 'Show Date

Again the app just closes down.

The default DateTime.Dateformat is "MM/dd/yyyy" and you are trying to parse a string "25/12/2014". Did you see the error?

In your b4a log should should have seen
java.text.ParseException: Unparseable date: "25/12/2014" (at offset 10)

To solve this you should:
- Remember the actual setting to set it back after your code
B4X:
Dim oldDateformat = DateTime.DateFormat
- Set the TimeFormat right to parse YOUR datestring
B4X:
DateTime.DateFormat = "dd/MM/yyyy"
- Do your calclulations
B4X:
Dim sDate As String = "25/12/2014" 'String Date
Dim lngDate As Long = DateTime.DateParse(sDate) 'Convert String to Long with Date Parse
DateTime.DateFormat = "dd MMM yyyy" 'Format Date
- Use it
B4X:
Log("Tag: "&DateTime.Date(lngDate))
'Msgbox(DateTime.Date(lngDate), "Date") 'Show Date
- Last but not least; set DateFormat back to it´s old Value!
B4X:
DateTime.DateFormat = oldDateformat


You could for sure use the right format for the date you want to parse with the default-settings of Dateutils.DateFormat....

B4X:
Dim sDate As String = "12/25/2014" 'String Date
should work in your code :D
 
Last edited:
Upvote 0

Jonathan Rigley

Member
Licensed User
Longtime User
Hi Manfred,

Thank you for the welcome, this is very new to me and its nice to meet helpful people.

As I understand, my date is not in the right format so needs to be changed before it can be parsed.

Dim oldDateformat = DateTime.DateFormat
DateTime.DateFormat = "dd/MM/yyyy"

Dim sDate As String = "25/12/2014" 'String Date
Dim lngDate As Long = DateTime.DateParse(sDate) 'Convert String to Long with Date Parse
DateTime.DateFormat = "dd MMM yyyy" 'Format Date
Log("Tag: "&DateTime.Date(lngDate))
DateTime.DateFormat = oldDateformat

This is what I tried but still with no luck, it threw an error on java.lang.NumberFormatException: Invalid double: "MM/dd/yyyy"

Hope you can help further.
Jonathan
 
Upvote 0

sirjo66

Well-Known Member
Licensed User
Longtime User
For me this works:
B4X:
  Dim oldDateformat As String = DateTime.DateFormat
   DateTime.DateFormat = "dd/MM/yyyy"
   Dim sDate As String = "25/12/2014" 'String Date
   Dim lngDate As Long = DateTime.DateParse(sDate) 'Convert String to Long with Date Parse
   DateTime.DateFormat = "dd MMM yyyy" 'Format Date
   Log("Tag: " & DateTime.Date(lngDate))
   DateTime.DateFormat = oldDateformat

No error.
On logs I see: 25 dic 2014

Sergio
 
Upvote 0

Jonathan Rigley

Member
Licensed User
Longtime User
For me this works:
B4X:
  Dim oldDateformat As String = DateTime.DateFormat
   DateTime.DateFormat = "dd/MM/yyyy"
   Dim sDate As String = "25/12/2014" 'String Date
   Dim lngDate As Long = DateTime.DateParse(sDate) 'Convert String to Long with Date Parse
   DateTime.DateFormat = "dd MMM yyyy" 'Format Date
   Log("Tag: " & DateTime.Date(lngDate))
   DateTime.DateFormat = oldDateformat

No error.
On logs I see: 25 dic 2014

Sergio
Thank you all for your help, I now see the date in the log.

Many thanks
Jonathan
 
Upvote 0

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
 
Upvote 0
Top