Current DayOfWeek issues with Calendar object under several ST accounts

I have some code that used to work fine, but has been acting up for the past 2 days…

def makeChange = false
Calendar localCalendar = Calendar.getInstance(TimeZone.getDefault())
int currentDayOfWeek = localCalendar.get(Calendar.DAY_OF_WEEK)

The above code is supposed to get the day of the week according to the local TimeZone.
Well, in several user locations (Pacific and East Coast Regions), it returns 4 (Wednesday) as
the current day of the week…And, today is Tuesday…

Must be a ST platform issue…

de31fea9-39cf-4a4b-a625-a6b0492a4695 9:53:47 PM: trace IsRightDayForChange>currentDayOfWeek=4 vs. dayOfWeek from Schedule 5=Wednesday

de31fea9-39cf-4a4b-a625-a6b0492a4695 9:53:47 PM: trace IsRightDayForChange>currentDayOfWeek=4, makeChange=false
de31fea9-39cf-4a4b-a625-a6b0492a4695 9:53:47 PM: trace IsRightDayForChange>currentDayOfWeek=4 vs. dayOfWeek from Schedule 4=Tuesday

Have you seen this issue elsewhere??? Has there been any change in the platform lately about the TimeZone processing?

tagging @slagle

If I do the following, I get the same results, According to ST, today is Wednesday, not Tuesday (wrong!)

Calendar currentDate=new GregorianCalendar()

String dayOfWeek = currentDate.getDisplayName( Calendar.DAY_OF_WEEK ,Calendar.LONG, Locale.getDefault())

36343fa5-c71d-4d42-acd1-cb85ca7f9494 10:38:31 PM: debug currentDayOfWeek=4 vs. dayOfWeek=Wednesday
36343fa5-c71d-4d42-acd1-cb85ca7f9494 10:38:31 PM: debug currentDayOfWeek=4

I use this code in webCoRE:

private utcToLocalDate(dateOrTimeOrString = null) {
	if (dateOrTimeOrString instanceof String) {
		//get UTC time
        try {        
			dateOrTimeOrString = timeToday(dateOrTimeOrString, location.timeZone).getTime()
        } catch (all) {
        	error "Error converting $dateOrTimeOrString to Date: ", null, null, all
        }
	}
	if (dateOrTimeOrString instanceof Date) {
		//get unix time
		dateOrTimeOrString = dateOrTimeOrString.getTime()
	}
	if (!dateOrTimeOrString) {
		dateOrTimeOrString = now()
	}
	if (dateOrTimeOrString instanceof Long) {
		return new Date(dateOrTimeOrString + location.timeZone.getOffset(dateOrTimeOrString))
	}
	return null
}

It converts a string to a date, or if no parameters passed, it returns the current date in local timezone.

I then use

utcToLocalDate().day

to get the current day of the week. The short-short version is:

long timestamp = now(); return (new Date(timestamp + location.timeZone.getOffset(timestamp))).day;

Funny, I still don’t see your posts on my computer :smiley: :smiley: :smiley:

4 Likes

Yeah, but the simpler way to get the right Day of the Week without any need to offset the date timestamp is

String currentDay = new Date().format("E", location.timeZone)

This will work under the ST platform. My point was that any call to Calendar.getInstance(TimeZone.getDefault()) will not work anymore. It used to work but something has changed in the ST platform and it doesn’t handle well the Calendar operations anymore in the local TimeZone.

1 Like

I won’t pretend to be an expert in working with dates/timezones, other than to know they are difficult, but wouldn’t using TimeZone.getDefault() be prone to errors? TimeZone.getDefault() will get the time zone of the server it is being executed on, which could be different than the time zone of the actual user’s location. And since the server’s time zone isn’t guaranteed to be the same as the time zone of the user’s location, that could lead to issues, I would think.

1 Like

@Jim Right you must be. Agree with you, I must. :wink:

1 Like