Android Question DateUtils Calculations

Declan

Well-Known Member
Licensed User
Longtime User
I need to calculate the time (period) a student spends reading an eBook.
In the Main Module I have a Process_Global variable:
B4X:
Dim MyBookStart As Long
When the eBook loads, I save to MyBookStart variable:
B4X:
        DateTime.DateFormat = "dd-MM-yyyy HH:MM:ss"
        Main.MyBookStart = DateTime.Date(DateTime.Now)
and receive this Error:
B4X:
java.lang.NumberFormatException: Invalid double: "03-09-2016 19:09:57"
 

imbault

Well-Known Member
Licensed User
Longtime User
You should try mm for minutes not MM:

DateTime.DateFormat = "dd-MM-yyyy HH:mm:ss"
Main.MyBookStart = DateTime.Date(DateTime.Now)

and mainly

DateTime.Date returns a String, not a Long
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Nope, still the same error.
I will need to convert the DateTime.Date string to a long, so that I can use:
B4X:
    Dim MyBookEnd As String
    Dim Diff As Period
    DateTime.DateFormat = "dd-MM-yyyy HH:mm:ss"
    MyBookEnd = DateTime.Date(DateTime.Now)

Diff=DateUtils.PeriodBetween(MyBookEnd, Main.MyBookStart)
 
Last edited:
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
Ok, but how do I achieve that?
From:
B4X:
    DateTime.DateFormat = "dd-MM-yyyy HH:mm:ss"
    MyBookEnd = DateTime.Date(DateTime.Now)
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
DateTime.Now returns Ticks (long)

if you want to process a

B4X:
Dim MyBookEnd As Long
Dim Diff As Period
MyBookEnd = DateTime.Now
Diff=DateUtils.PeriodBetween(Main.MyBookStart, MyBookEnd)

You have just to convert or to store Ticks in Main.MyBookStart
 
Upvote 0
Top