Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to fetch the logged user timezone in groovy

Vineela Durbha
Contributor
January 29, 2019

I am trying to set the datetime field for an issue by picking the value from another issue.

Problem I am facing is that I am able to set the value but with different timezone. Is there any way I can set the time zone to the loggedin user.

I am trying with below code

import com.atlassian.jira.timezone.TimeZoneManager;
import java.util.TimeZone

TimeZone timeZone = timeZoneManager.getLoggedInUserTimeZone();

simpleDateFormat.setTimeZone(timeZone);

 

I am getting groovy.lang.MissingPropertyException: No such property: timeZoneManager for class

 

Can anyone help in resolving this

1 answer

1 accepted

1 vote
Answer accepted
AF February 7, 2020
/* -the old API has changed, 
regrettably this is an old question
but I hope my answer helps other people
*/

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.timezone.TimeZoneManager

// -if you need the base TZ object

def userTimeZone = ComponentAccessor.getComponent(TimeZoneManager).getLoggedInUserTimeZone()

// -if you need the TZ string to pass to a date

def userTimeZoneValue = ComponentAccessor.getComponent(TimeZoneManager).getLoggedInUserTimeZone()
.getProperties()['lastRule'].getProperties()['ID'].toString()

// -sample usage

def date = new Date().format('yyyy-MM-dd hh:mm:ss', TimeZone.getTimeZone(userTimeZoneValue))

// -check the output

return [userTimeZoneValue, date]

/* -this URL helped me  https://innovalog.atlassian.net/wiki/spaces/JMWE/pages/164495395/Code+snippet+to+fetch+the+current+user+s+time+zone
*/
Matt Doar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 12, 2020

Thanks, that was helpful

Like AF likes this
subham patra
Contributor
January 28, 2021

Hi @AF  - I tried with the given snippet, It works to fetch the date but not getting UserTimeZoneValue. Any help would be appreciated. Thank you! 

AF February 3, 2021

Hi Subham,

Sorry that I just saw this. I tested it and still works for me.

If you change the return statement to this:

return ComponentAccessor.getComponent(TimeZoneManager)
.getLoggedInUserTimeZone().getProperties()

Do you get any output? 

If yes, does it contain any property named like "lastRule" or similar? 

If not, what version of the Scriptrunner plugin and Jira are you running?

Also, please check that your user profile (in the UI) is set to your desired TimeZone. I haven't tested it for one which is unset. I am not sure that default values would behave the same as custom ones.

Hope it helps.

Suggest an answer

Log in or Sign up to answer