Hi,
I would like to know what code i would need to retrieve what day today is as a written day.
So today is thursday. I’ve seen many topics about retrieving dates but that is usually like sysdate (09/16/2021)
Please note i am not a programmer. I really don’t understand code much so please be specific.
But i guess it should be written like something like this:
def daytoday= new Date().format(‘daywrittenout’)
WebUI.verifyElementText(findTestObject(‘Kalenderday’), daytoday + ’ is my favourite day of the week.’)
How about something like below?
def todaysDate = new Date()
"list out the days of the week"
List<String> daysWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
"get which day it is"
int date = todaysDate[Calendar.DAY_OF_WEEK] - 1
"get the actual day of the week"
def daytoday = daysWeek[date]
"check if we compare to text"
WebUI.verifyElementText(findTestObject('Kalenderday'), "${daytoday} is my favourite day of the week.")
Reading on Calendar “first day of the week”, it seems to depend on where you live, so you can do some testing and get back to us on what you find.
Yeah man you are awesome! That was exactly what i needed AND i got it to work first time.
Many thanks!
I live in the Netherlands. I guess it’s a PC setting what the first day of the week is? I think it’s set for me as it did return tuesday. I guess if your week begins on monday you just set the array to start with monday instead?
I’m having some fun with the code. Learn to understand what is happening.
I add a line to the code
int date1 = (todaysDate[Calendar.DAY_OF_WEEK]) + 2 (So if i run the test on a tuesday it should return sunday)
But instead it returns NULL. I guess because the array ends with saturday. ([“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”])
So how should i solve this? Like, regardless of what day of the week it is. I should be able to do return today and also tomorrow (‘Today’ is my favorite day of the week, but i hate ‘tomorrow’)
Unfortunately, the Day of the Week does not cycle around. The count only goes from 1 to 7 (or 0 to 6) and if you go over that number, the result does not make any sense to the computer. (If you are familiar with Math, it’s like dividing by zero–it’s just not done.)
So if you have to do something like that, you have to be careful and set it up that if count goes over, to reset back to the beginning and start again. I will re-read what you are doing and see if I can suggest something.
Also, here is another method to find the day of the week:
import java.text.SimpleDateFormat
def daytoday = new SimpleDateFormat("EEEE").format(Calendar.getInstance().getTime())
"check if we compare to text"
WebUI.verifyElementText(findTestObject('Kalenderday'), "${daytoday} is my favourite day of the week.")
Another example that uses the Java8 Date/Time API.
import java.time.DayOfWeek
import java.time.LocalDate
import java.time.format.DateTimeFormatter
LocalDate today = LocalDate.now()
println "today is ${today.format(DateTimeFormatter.ISO_DATE)}" // today is 2021-09-17
DayOfWeek dow = today.getDayOfWeek()
println "today's DayOfWeek is ${dow.toString()}" // today's DayOfWeek is FRIDAY
LocalDate yesterday = today.minusDays(1)
println "yesterday's DayOfWeek is ${yesterday.getDayOfWeek().toString()}" // yesterday's DayOfWeek is THURSDAY
LocalDate tomorrow = today.plusDays(1)
println "tomorrow's DayOfWeek is ${tomorrow.getDayOfWeek().toString()}" // tomorrow's DayOfWeek is SATURDAY
LocalDate lastDayOfThisYear = LocalDate.of(today.getYear(), 12, 31)
println "DayOfWeek of 31 Dec this year is ${lastDayOfThisYear.getDayOfWeek().toString().toLowerCase().capitalize()}" // Friday
/**
* Tutorial: https://www.baeldung.com/java-8-date-time-intro
*/
/**
* Javadoc: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
*/
java.util.Date
, java.util.Calendar
— these are old API since JDK1.1 released 24 years ago. They work, of course, but are not worth learning. A Java learner today should learn the newer/better Java8 Date/Time API (java.time.*
). Have a look at the following tutorial.
Oh wow thanks everyone. I will be sure to learn from that tutorial and your example codes. Thanks a bunch!