Date time formats in Velocity script | Community
Skip to main content
Jon_Wright
New Participant
October 19, 2022
Solved

Date time formats in Velocity script

  • October 19, 2022
  • 1 reply
  • 5270 views

Hi

I'm working with date times in Velocity and struggling to get a date to display in the correct format. I have an incoming date string in the format: '2023-04-04T13:00:00Z'. So the incoming time is 1300 UTC which is 1400 BST

 

I'm using @sanfordwhiteman examples from here: https://blog.teknkl.com/velocity-days-and-weeks/

 

And wanted to get the date to display as:

Tuesday 4 April 14:00 BST

 

But it displays as 13:00 BST instead. Here is my sample code, note the line:

#set( $ISO8601DateTimeUTC = "yyyy-MM-dd'T'HH:mm:ss'Z'" )

is not taken from Sanford's blog which I think is the problem, as it seems it's not recognising the format at UTC and rather just a TZ unaware format. Full sample code:

#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("Europe/London") ) #set( $defaultLocale = $date.getLocale() ) #set( $calNow = $date.getCalendar() ) #set( $ret = $calNow.setTimeZone($defaultTimeZone) ) #set( $calConst = $field.in($calNow) ) #set( $ISO8601DateOnly = "yyyy-MM-dd" ) #set( $ISO8601DateTime = "yyyy-MM-dd'T'HH:mm:ss" ) #set( $ISO8601DateTimeUTC = "yyyy-MM-dd'T'HH:mm:ss'Z'" ) #set( $ISO8601DateTimeWithSpace = "yyyy-MM-dd HH:mm:ss" ) #set( $ISO8601DateTimeWithMillisUTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" ) #set( $ISO8601DateTimeWithMillisTZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ) ##set input date #set($input_date = '2023-04-04T13:00:00Z') ##convert input date #set( $input_date = $convert.toCalendar( $convert.parseDate( $input_date, $ISO8601DateTimeUTC, $defaultLocale, $defaultTimeZone ) ) ) ##set date formats #set($format_display = "EEEE d MMMM HH:mm z" ) ##OUTPUT Display format:<br/> $date.format($format_display,$input_date,$defaultLocale,$defaultTimeZone)

  Thanks!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by SanfordWhiteman

'Z' in a format string is just a letter (not keyword), you wanted Z.

#set( $utcTimeZone = $date.getTimeZone().getTimeZone("UTC") ) #set( $defaultTimeZone = $date.getTimeZone().getTimeZone("Europe/London") ) #set( $defaultLocale = $date.getLocale() ) #set( $calNow = $date.getCalendar() ) #set( $ret = $calNow.setTimeZone($defaultTimeZone) ) #set( $calConst = $field.in($calNow) ) #set( $ISO8601DateOnly = "yyyy-MM-dd" ) #set( $ISO8601DateTime = "yyyy-MM-dd'T'HH:mm:ss" ) #set( $ISO8601DateTimeUTC = "yyyy-MM-dd'T'HH:mm:ss" ) #set( $ISO8601DateTimeWithSpace = "yyyy-MM-dd HH:mm:ssZ" ) #set( $ISO8601DateTimeWithMillisUTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" ) #set( $ISO8601DateTimeWithMillisTZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ) ##set input date #set($input_date_string = "2023-04-04T13:00:00Z") ##convert input date #set( $input_date = $convert.toCalendar( $convert.parseDate( $input_date_string, $ISO8601DateTimeUTC, $defaultLocale, $utcTimeZone ) ) ) #set($format_display = "EEEE d MMMM HH:mm z" ) ##OUTPUT Display format:<br/> $date.format($format_display,$input_date,$defaultLocale,$defaultTimeZone)

 

 Also note I don’t like resetting the date-like string variable to the real Calendar object so I used different var names.

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
October 19, 2022

'Z' in a format string is just a letter (not keyword), you wanted Z.

#set( $utcTimeZone = $date.getTimeZone().getTimeZone("UTC") ) #set( $defaultTimeZone = $date.getTimeZone().getTimeZone("Europe/London") ) #set( $defaultLocale = $date.getLocale() ) #set( $calNow = $date.getCalendar() ) #set( $ret = $calNow.setTimeZone($defaultTimeZone) ) #set( $calConst = $field.in($calNow) ) #set( $ISO8601DateOnly = "yyyy-MM-dd" ) #set( $ISO8601DateTime = "yyyy-MM-dd'T'HH:mm:ss" ) #set( $ISO8601DateTimeUTC = "yyyy-MM-dd'T'HH:mm:ss" ) #set( $ISO8601DateTimeWithSpace = "yyyy-MM-dd HH:mm:ssZ" ) #set( $ISO8601DateTimeWithMillisUTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" ) #set( $ISO8601DateTimeWithMillisTZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ) ##set input date #set($input_date_string = "2023-04-04T13:00:00Z") ##convert input date #set( $input_date = $convert.toCalendar( $convert.parseDate( $input_date_string, $ISO8601DateTimeUTC, $defaultLocale, $utcTimeZone ) ) ) #set($format_display = "EEEE d MMMM HH:mm z" ) ##OUTPUT Display format:<br/> $date.format($format_display,$input_date,$defaultLocale,$defaultTimeZone)

 

 Also note I don’t like resetting the date-like string variable to the real Calendar object so I used different var names.

Jon_Wright
New Participant
October 19, 2022

Thank you (again) Sanford!  So convert to UTC then display in Europe/London.

Just 1 thing, in your example you set:

#set( $ISO8601DateTimeUTC = "yyyy-MM-dd'T'HH:mm:ss" )

Without a Z, is that an omission? It seems to work both with and without, so I'm guessing this is a rare case of Velocity being a bit forgiving?

 

SanfordWhiteman
New Participant
October 19, 2022

The date format is only parsed up until the end (i.e. the format can be shorter than the data) and because you want UTC you don’t actually need the keyword Z. Course that means it’s the same format as $ISO8601DateTime so you might as well just use that!