groovy -- having problems getting date in human readable format -- tips?

Hi guys,

How do I get human readable date from this? I can't get it to work. The closest I can get is the last login information in millisecond time?

import com.atlassian.jira.bc.security.login.LoginService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.api.UserWithAttributes
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import java.sql.Timestamp



def result = ""

def groupNames = [
        'jira-users',
]

CrowdService crowdService = ComponentAccessor.crowdService
UserWithAttributes user
UserUtil userUtil = ComponentAccessor.userUtil
LoginService loginService = ComponentAccessor.getComponent(LoginService)


userUtil.getAllUsersInGroupNamesUnsorted(groupNames).findAll { it.isActive() }.each {
    user = crowdService.getUserWithAttributes(it.getName())


    Long lastLoginTime = loginService.getLoginInfo(user.name).getLastLoginTime()

    if (lastLoginTime != NULL) {
        Timestamp lastLoginTimeStamp = new Timestamp(lastLoginTime as long);
        result += "User ${user.name} logged in: " + lastLoginTimeStamp + "\r\n"
    } else {
        result += "User ${user.name} logged in: " + "\t" + 'NULL' + "\r\n"


    }

}



}
return result

--tips appreciated!

3 answers

1 accepted

This widget could not be displayed.

Hi,

This script runs well for me. It shows timestamp in human readable format.

import com.atlassian.jira.bc.security.login.LoginService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.crowd.embedded.api.CrowdService
import com.atlassian.crowd.embedded.api.UserWithAttributes
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import java.sql.Timestamp

def result = ""

def groupNames = [
'jira-users',
]

CrowdService crowdService = ComponentAccessor.crowdService
UserWithAttributes user
UserUtil userUtil = ComponentAccessor.userUtil
LoginService loginService = ComponentAccessor.getComponent(LoginService)

userUtil.getAllUsersInGroupNamesUnsorted(groupNames).findAll { it.isActive() }.each {
user = crowdService.getUserWithAttributes(it.getName())

if (loginService.getLoginInfo(user.name).getLastLoginTime() > 0) {
Long lastLoginTime = loginService.getLoginInfo(user.name).getLastLoginTime()

Timestamp lastLoginTimeStamp = new Timestamp(lastLoginTime as long);
result += "User ${user.name} logged in: " + lastLoginTimeStamp + "\r\n"
}else {
        result += "User ${user.name} logged in: " + "\t" + 'NULL' + "\r\n"
 
 
    }
}
result

Let me know if it doesnt work for you.

Vijay

Thanks Vijay -- this works for me too.

This widget could not be displayed.

Hi Bryan,

The timestamp is stored in milliseconds from "the beginning of time".

You could try the solution in this post: http://stackoverflow.com/questions/3371326/java-date-from-unix-timestamp

Cheers,

// Svante

This widget could not be displayed.

new Date(lastLoginTimeStamp)

will give you a human date, as it will be implicity toString'd. You might want to use a DateFormat depending on your meaning of "human readable".

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

286 views 5 0
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you