Android Question Time

Pilar-JLSineriz

Active Member
Licensed User
Longtime User
Hello

I want to do anything that I think that it's simply; to write the time (p.e. 23:10) in an edittext and to storage as date..
Thanks
 

sonicmayne

Member
Licensed User
Longtime User
This will take a 24 hour string and parse it to a date, then write the result to a file.
B4X:
    Dim date As String = "23:10"
    Dim format As String = "HH:mm"
    Dim oformat As String = DateTime.DateFormat
    DateTime.DateFormat = format
    Dim parseddate As Long = DateTime.DateParse(date)
    DateTime.DateFormat = oformat
    File.WriteString("yourpath","yourfile",parseddate)
 
Upvote 0

Pilar-JLSineriz

Active Member
Licensed User
Longtime User
Thanks
.. I use your code.. I see that transform HH:mm in ticks.. but have I the possibility to put a mask in the edittext so the user only can write in format "HH:mm"???
 
Upvote 0

sonicmayne

Member
Licensed User
Longtime User
I haven't been able to test this, but this should work (requires the IME library):
This will set the mask.
B4X:
    Dim IME As IME
    IME.SetCustomFilter(Txt,Txt.INPUT_TYPE_TEXT,"0123456789:")

And when you are ready to validate the input:
B4X:
    Dim Matcher As Matcher = Regex.Matcher("(\d\d):(\d\d)",txt.Text)
    Do While Matcher.Find
        Dim hour As Int = Matcher.Group(1)
        Dim minute As Int = Matcher.Group(2)
        If hour < 0 OR hour > 23 Then
            Log("Invalid Hour!")
            Return
        End If
        If minute < 0 OR minute > 59 Then
            Log("Invalid Minute!")
            Return
        End If
        'Valid time! Do somthing here.
    Loop
 
Upvote 0

Pilar-JLSineriz

Active Member
Licensed User
Longtime User
... but if the user writes, for example, 2315 (without ":") there isn´t log with the error.. thanks for your help!!
 
Upvote 0

sonicmayne

Member
Licensed User
Longtime User
B4X:
Dim Matcher As Matcher = Regex.Matcher("^(\d\d):(\d\d)$",txt.Text)
    Do While Matcher.Find
        Dim hour As Int = Matcher.Group(1)
        Dim minute As Int = Matcher.Group(2)
        If hour < 0 OR hour > 23 Then
            Log("Invalid Hour!")
            Return
        End If
        If minute < 0 OR minute > 59 Then
            Log("Invalid Minute!")
            Return
        End If
        'Valid time! Do somthing here.
        Return
    Loop
    Log("Invalid format!")
 
Upvote 0

sonicmayne

Member
Licensed User
Longtime User
What happens?
It works here.

Is it the Unreachable code detected warning? If so add 'ignore at the end of the Loop line.

So it reads: Loop 'ignore
 
Upvote 0

Pilar-JLSineriz

Active Member
Licensed User
Longtime User
it's Ok... thanks thanks... I am going to try sleep... the temperature is so high in Madrid (during the day 41ºC....)

(your code with sentences in spanish!!!)
Dim Matcher As Matcher = Regex.Matcher("(\d\d):(\d\d)",txtHORA.Text)
Do While Matcher.Find
Dim hour As Int = Matcher.Group(1)
Dim minute As Int = Matcher.Group(2)
If hour < 0 Or hour > 23 Then
ToastMessageShow("HORA NO Válida, tiene que estar comprendida entre 0 y 23.",True)
txtHORA.Text=DateTime.Time(DateTime.Now)
Return
End If
If minute < 0 Or minute > 59 Then
txtHORA.Text=DateTime.Time(DateTime.Now)
ToastMessageShow("MINUTOS NO Válidos, tienen que estar comprendidos entre 0 y 59.",True)
Return
End If
Return
Loop
ToastMessageShow("Formato de hora inválido",True)
 
Upvote 0

Pilar-JLSineriz

Active Member
Licensed User
Longtime User
Hi... could you help me again???? thanks

If I have one edittext with a date, p.e. 9/07/2015 and other with one time, p.e. 10:12, and two more editexts, with 10/07/2015 and 15:13, how I can calculate the minutes that there are between de dates???? Diference in minutes between 10/07/2015 15:13 and 9/07/2015 15:13
 
Upvote 0

Eumel

Active Member
Licensed User
Longtime User
B4X:
    Dim startdate As String
    Dim starttime As String
    Dim enddate As String
    Dim endtime As String
    Dim start As Long
    Dim finish As Long
   
    Dim per As Period
   
    DateTime.DateFormat = "dd/MM/yyyy"
    DateTime.TimeFormat = "HH:mm"
   
    startdate = "09/07/2015"
    starttime = "10:12"
    enddate = "10/07/2015"
    endtime = "15:13"
   
    per = DateUtils.PeriodBetween(DateTime.DateTimeParse(startdate, starttime), DateTime.DateTimeParse(enddate, endtime))
   
    Log(per)
    Log((per.Days*24*60)+(per.Hours*60)+(per.Minutes)) 'calculates from given days/hours/minutes to minutes
   

   
    'another way
   
    start = DateTime.DateTimeParse(startdate, starttime)
    finish = DateTime.DateTimeParse(enddate, endtime)
   
    Dim difference As Long = finish - start 'here the ticks from difference
   
    Dim dif As Int = difference / 1000 / 60 'seconds = 1000 milliseconds
                                            'minutes = 60 seconds
   
    Log(dif)


quick and dirty but i hope it helps
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    DateTime.DateFormat = "dd/MM/yyyy"
    DateTime.TimeFormat = "HH:mm"
End Sub
Sub Button1_Click
    Dim startdate As Long = DateTime.DateTimeParse(editStartDate.Text,editStartTIME.Text)   
    Dim enddate As Long = DateTime.DateTimeParse(EditEndDATE.Text,EditEndTIME.Text)   
    Dim p As Period = DateUtils.PeriodBetween(startdate,enddate)
    LabelResult.Text = $"${p.Hours}H, ${p.Minutes}minutes"$
End Sub
 

Attachments

  • PilarDateEx.zip
    7.6 KB · Views: 193
Upvote 0

Pilar-JLSineriz

Active Member
Licensed User
Longtime User
I've solved as follow (lblADP and lblADP1 are labels from AnotherDatepicker)

Sub btnMinutos_Click

Dim IniHORA As String = txtHORA.Text
Dim FinHORA As String = txtHORA1.Text

Dim IniDIA As Long = lblADP.Text
Dim FinDIA As Long = lblADP1.Text

Dim lngTimeFrom As Long = DateTime.DateTimeParse(DateTime.Date(IniDIA),IniHORA)
Dim lngTimeTo As Long = DateTime.DateTimeParse(DateTime.Date(FinDIA),FinHORA)

Dim TimeDiff As Period= DateUtils.PeriodBetween(lngTimeFrom, lngTimeTo)

Dim intDias As Int = TimeDiff.Days
Dim intHoras As Int = TimeDiff.Hours
Dim intMinutos As Int= TimeDiff.Minutes

lblTimeP1.Text=intDias*24*60 + intHoras*60+ intMinutos
End Sub
 
Upvote 0

Similar Threads

Top