How to format a DateTime attribute in the view?

Francesco Malvezzi francesco.malvezzi at unimore.it
Thu Mar 14 10:58:28 UTC 2024



On 13/03/24 13:41, Cantor, Scott wrote:
>> I would like to format and localize it. Can anybody help me to solve
>> this detail?
> 
> The IdP since V4 uses the standard Java classes introduced in, I want to say 8, for date/time handling.
> 
> The underlying attribute value type is java.time.Instant, per [1] (that's a 5.1 link, but the same class is in 4.x).
> 
> The relevant reference for the Formatting class in Java 11 is [2].
> 
> In a nutshell, you build the Formatter you want, and pass the getValue() result into it to get a string out.

thank you for your help, I was really light-years away.

My working draft is now:

#set ($rpContext = 
$profileRequestContext.getSubcontext('net.shibboleth.idp.profile.context.RelyingPartyContext'))
#set ($attrContext = 
$rpContext.getSubcontext('net.shibboleth.idp.attribute.context.AttributeContext'))
#set ($expirations = 
$attrContext.getUnfilteredIdPAttributes().get("examplePwdExpiration").getValues())
#set ($expiration = $expirations.get(0).getValue())
#set ($info = "hello world")
#set ($zoneOffset = 
$info.getClass().forName("java.time.ZoneOffset").systemDefault())
#set ($dateTime = 
$info.getClass().forName("java.time.LocalDateTime").ofInstant($expiration, 
$zoneOffset))
#set ($dateStyle = 
$info.getClass().forName("java.time.format.FormatStyle").valueOf("MEDIUM"))
#set ($formatter = 
$info.getClass().forName("java.time.format.DateTimeFormatter").ofLocalizedDateTime($dateStyle))
#set ($formattedDate = $dateTime.format($formatter))

where examplePwdExpiration is the SAML2DateTime attribute pulled out 
from OpenLDAP.

The example output of $formattedData is:

Mar 24, 2024, 3:04:00 PM

Is my experiment close to your expectations?

thank you for your time,

Francesco



More information about the users mailing list