On 09/28/2011 05:20 PM, Legeinfo wrote:
> I want to split a dato string like 2011.08.02 into year, month and days to
> calculate the age of a person.
> $year = 2011
> $month = 08
> $day = 02
>
> $year =($date.replaceAll($'\d\d\d\d.','$1'))
> is not working.

I'm not sure what you tried to write there, but it doesn't look right.

There are many ways to do this. When you said "split", the most 
straight-forward way to do this seems to be:

#set ($date = "2011.08.02")
#set ($datePieces = $date.split('\.'))
$datePieces[0] $datePieces[1] $datePieces[2]

If you really want a solution based on replaceAll, then this seems to work:

#set ($date = "2011.08.02")
#set ($year = $date.replaceAll('(\d{4})\..*', '$1'))
#set ($month = $date.replaceAll('\d{4}\.(\d{1,2})\..*', '$1'))
#set ($day = $date.replaceAll('.*\.(\d{2})$', '$1'))
$year $month $day

The most flexible way would be to use the jodatime plugin to parse this 
string into a real DateTime object, from which you can request the 
various date parts explicitly.

#set ($date = "2011.08.02")
#set ($dateObj = 
$xwiki.jodatime.getDateTimeFormatterForPattern('yyyy.MM.dd').parseDateTime($date))
$dateObj.year $dateObj.monthOfYear $dateObj.dayOfMonth

-- 
Sergiu Dumitriu
http://purl.org/net/sergiu/
_______________________________________________
users mailing list
users@xwiki.org
http://lists.xwiki.org/mailman/listinfo/users

Reply via email to