Android Question Convert DatePeriod to string

Discussion in 'Android Questions' started by trueboss323, Feb 23, 2015.

  1. trueboss323

    trueboss323 Active Member Licensed User

    Dim span as Period
    I want to get the time difference between 2 times.
    I tried using on a button click
    Dim span2 As String
    span = DateUtils.PeriodBetween(StartTime, endTime)
    span2 = span
    and then the message box shows:
    java.lang.NumberFormatException: Invalid double: "[Days=0, Hours=0, IsInitialized=true, Minutes=0, Months=0, Seconds=14, Years=0]"

    How can I change it so it appears as 00:00:14 ?
  2. Erel

    Erel Administrator Staff Member Licensed User

    You need to convert the period to such formatted string. It will not happen automatically.
    Sub PeriodToString(p As PeriodAs String
    Return $"$2{p.Hours}:$2{p.Minutes}:$2{p.Seconds}"$
    End Sub
  3. DonManfred

    DonManfred Expert Licensed User

    span2 = numberformat(span.Hours,2,0)&":"&numberformat(span.Minutes,2,0)&":"&numberformat(span.Seconds,2,0)
  4. trueboss323

    trueboss323 Active Member Licensed User

    Thanks Erel. One more question.
    Is it possible to check if the time span is greater than another time span? I already used

    'Check if span is greater
    If PeriodToString(span) > StateManager.GetSetting("Prevtime"Then
    Msgbox("You have beat your record","")
    End If
  5. Erel

    Erel Administrator Staff Member Licensed User

    It is possible, but you will need to write the code that checks it. If you are only dealing with hours or smaller intervals then it is quite simple to convert the Period to seconds and then compare it:
    Sub ShortPeriodToSeconds(p As PeriodAs Int
    Return p.Hours * 3600 + p.Minutes * 60 + p.Seconds
    End Sub
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice