Android Question Getting day of week

Sergio83

Member
Licensed User
Longtime User
Hello everybody,

I'm facing an issue regarding the way to get the day of week depending on Android version. I googled as I can do, but with no usefull result, may be my search is not in the right scope ...

For some personal reasons I use some very new and also some very old new-stock smartphones (at a very low price) for my home automation system, but the installed Android version is also old new-stock: 4.4.4 in the second case.

The problem is when I try to get the day of week with "DateTime.GetDayOfWeek(DateTime.Now)" depending of Andorid version I get a different value (one day shift ...).

I'm probably wrong somewhere (may be the SDK version), but if anyone could help me to fix or turnaround this issue it will be very appreciate

Try to have the most beautifful summer days all of you !

(My environment : very latest version of B4A and SDK 29)
 

Alexander Stolte

Expert
Licensed User
Longtime User
The problem is when I try to get the day of week with "DateTime.GetDayOfWeek(DateTime.Now)" depending of Andorid version I get a different value (one day shift ...).
The problem will be that on the phones where it is shifted by 1 day, a different time zone will be set. On one phone the week starts on Monday, on the other on Sunday.

The solution is in the caption of the GetDayOfWeek function.

You need this lib.

but i don't know, how to change the first day of week, with this lib....
 
Upvote 0

Sergio83

Member
Licensed User
Longtime User
Hello Alexander,

You're right it is just a time zone setting issue, I used to live both in France and Portugal and I have to take care of that when settint my smartphones. I'm already using AHlocate and now how to set the first day of week.
Many thanks to have put me in a deeper thinking situation regarding this pseudo issue!

Have a good summer sunny day !!!
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
On the phone your test app shows incorrect week day number, the date and time are shown correctly? If not, fix the problem first; make sure the timezone, date and time are all correct.

The following code should show same results regardless of the version of android or first day of week:
B4X:
    log(DateTime.GetDayOfWeek(DateTime.Now))
    Log(DateUtils.GetDayOfWeekName(DateTime.Now)) 'depends on DateUtils library
 
Upvote 0

derez

Expert
Licensed User
Longtime User
B4X:
'using these:
'Dim t As Long = (DateTime.now)
'year = DateTime.GetYear(t)
'month = DateTime.GetMonth(t) 
'day= DateTime.GetDayOfMonth(t) 


Sub DayOfTheWeek As Int  ' Sunday = 1
    Private qx, mi, y, p  As Int

    If month < 3 Then y = 1999 + year  Else y = 2000 + year
    qx = (month+9) Mod(12) +1
    mi = Floor(2.6*qx-0.2)
    p = y/4
    Return (day + mi  + y + p -1 )Mod 7
End Sub
 
Last edited:
Upvote 0
Top