B4J Question DateTime

moore_it

Well-Known Member
Licensed User
Longtime User
Hi all,

i've a problem with DateTime, when i use this code

Dim c As Int = DateTime.GetDayOfWeek(DateTime.DateParse(pData))

where pdata = 2015 11 01
c = 5 (sunday) and it's true
when i pass pData = 2015 10 01
c = 5 (sunday) and it's false the true value is 2

why ?
where i wrong ?

This is the log

Program started.
1420067460000
2015 11 01
5
1420067400000
2015 10 01
5

Thanks
 

moore_it

Well-Known Member
Licensed User
Longtime User
B4X:
Sub draw(pData As String)
   tela.ClearRect(0,0,tela.Width,tela.Height)
   DateTime.DateFormat = "yyyy mm dd"
   Dim r As Int = 1
   For i = 0 To 6
     tela.DrawText(DayNames(i),24+(28*i),18,fx.DefaultFont(10),fx.Colors.Blue,"RIGHT")
   Next
   Log (DateTime.dateparse(pData))
   Dim c As Int = DateTime.GetDayOfWeek(DateTime.DateParse(pData)) + 1
   Log(pData)
   Log(c)
   For i = 1 To DaysOfMonth(aa,mm)
     If c > 6 Then
       c = 0
       r = r + 1
     End If
     If c = 6 Then
       tela.DrawText(i,24+(28*c),18+(22*r),fx.DefaultFont(12),fx.Colors.Red,"RIGHT")
     Else    
       tela.DrawText(i,24+(28*c),18+(22*r),fx.DefaultFont(12),fx.Colors.Black,"RIGHT")
     End If
     c = c + 1
   Next
   lAnno.Text = aa    
   lMese.Text = MonthNames(mm-1)
End Sub

where pData is a string in this format "2015 11 01"

Thanks in advice
Toni
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
you need "yyyy MM dd" as the format not "yyyy mm dd"
(I think it uses minutes when you specify mm)
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
I have a similar problem converting a date time (looks alright to me):

This is my method:
B4X:
Sub DateFormat2(Date As String) As String
    Log(Date)
    Dim lng As Long
    Dim OrigFormat As String=DateTime.DateFormat
    DateTime.DateFormat = "yyyy-MM-dd"
    DateTime.DateFormat = "HH:mm:ss"
    Dim d As String = Date.SubString2(0,10)
    Dim t As String = Date.SubString2(11,19)
    Log(d)
    Log(t)
    lng = DateTime.DateTimeParse(d,t)  ' <----- crash here
    DateTime.DateFormat = "ddd dd-MM-yy hh:nn"
    Dim ret As String = DateTime.Date(lng)  
    DateTime.DateFormat=OrigFormat
    Return ret
End Sub

the log:

B4X:
SELECT tEvent.evContact, tEvent.evDatum, tPersoon.prsCode, tEventStatus.evstatCode, tEventType.evtypOms, tEvent.evOnderwerp, tEvent.evCommentaar
               FROM ((tEvent INNER JOIN tEventType ON tEvent.evType = tEventType.evtypID) INNER JOIN tEventStatus ON tEvent.evStatus = tEventStatus.evstatID) INNER JOIN tPersoon ON tEvent.evToDoBy = tPersoon.prsID
               WHERE (((tEvent.evContact) = 15734))
               ORDER BY tEvent.evDatum desc;
2015-10-28 12:57:07.0
2015-10-28
12:57:07
Error occurred on line: 138
java.text.ParseException: Unparseable date: "2015-10-28"
    at java.text.DateFormat.parse(DateFormat.java:366)
    at anywheresoftware.b4a.keywords.DateTime.DateParse(DateTime.java:112)
    at anywheresoftware.b4a.keywords.DateTime.DateTimeParse(DateTime.java:154)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:593)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:225)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:158)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
    at anywheresoftware.b4a.debug.Debug.delegate(Debug.java:55)
    at com.onetwo.abmshared._dateformat2(abmshared.java:324)
    at com.onetwo.prospectpage._gm1_markerclicked(prospectpage.java:566)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:593)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:158)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
    at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:462)
    at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:417)
    at com.onetwo.prospectpage._page_parseevent(prospectpage.java:693)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:593)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:228)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:158)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
    at anywheresoftware.b4j.object.WebSocketModule$Adapter$1.run(WebSocketModule.java:126)
    at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
    at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
    at anywheresoftware.b4a.ShellBA.startMessageLoop(ShellBA.java:111)
    at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:131)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:300)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:158)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
    at com.onetwo.main.main(main.java:29)
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
It doesn't look alright to me. In the 6th line of your sub, you're setting a date format to something that looks more like a time format. That's why the DateTime object can't parse the date.

Try changing it to
B4X:
DateTime.TimeFormat = "HH:mm:ss"
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…