Joda Time Plugin

Last modified by Vincent Massol on 2021/03/17 21:02

cogProvides a Velocity-friendly API on top of the Joda Time library
TypePlugin
Category
Developed by

XWiki Development Team

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1
Bundled With

XWiki Standard

Description

The API is the following:

/**
 * @see org.joda.time.DateTime#DateTime()
 */
public DateTime getDateTime();

/**
 * @see org.joda.time.DateTime#DateTime(int, int, int, int, int, int, int)
 */
public DateTime getDateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond);

/**
 * @see org.joda.time.DateTime#DateTime(long)
 */
public DateTime getDateTime(long instant);

/**
 * @see org.joda.time.MutableDateTime#MutableDateTime()
 */
public MutableDateTime getMutableDateTime();

/**
 * @see org.joda.time.MutableDateTime#MutableDateTime(int, int, int, int, int, int, int)
 */
public MutableDateTime getMutableDateTime(int year, int monthOfYear, int dayOfMonth, int hourOfDay, int minuteOfHour, int secondOfMinute, int millisOfSecond);

/**
 * @see org.joda.time.MutableDateTime#MutableDateTime(long)
 */
public MutableDateTime getMutableDateTime(long instant);

/**
 * @see org.joda.time.format.DateTimeFormat#forPattern(String)
 */
public DateTimeFormatter getDateTimeFormatterForPattern(String pattern);

/**
 * @see org.joda.time.format.DateTimeFormat#forStyle(String)
 */
public DateTimeFormatter getDateTimeFormatterForStyle(String style);

/**
 * @return an ISO8601 date time formatter
 */
public DateTimeFormatter getISODateTimeFormatter();

Example

## Example 1
#set($now = $xwiki.jodatime.dateTime)
#set($weekStart = $now.withDayOfWeek(1))
#set($weekEnd = $now.withDayOfWeek(7))
#set($formatter = $xwiki.jodatime.getDateTimeFormatterForStyle('F-'))
First day of the current week is $formatter.print($weekStart)

Last day of the current week is $formatter.print($weekEnd)

## Example 2
#set($formatter = $xwiki.jodatime.getDateTimeFormatterForPattern("dd/MM/yyyy 'at' hh:mm"))
#set($creationDate = $xwiki.jodatime.getDateTime($doc.creationDate.time))
Document created on $formatter.print($creationDate)

## Example 3
#set($formatter = $xwiki.jodatime.getDateTimeFormatterForPattern('yyyy.MM.dd'))
#set($myBirthday = $formatter.parseDateTime('2007.11.25'))
#set($daysLeft = $myBirthday.minus($now.millis))
$daysLeft.getDayOfYear() days left till my birthday!

## Example 4
#set($formatter = $xwiki.jodatime.getDateTimeFormatterForPattern("dd/MM/yyyy"))
#set($nextDay = $xwiki.jodatime.mutableDateTime)
#set($discard = $nextDay.setMillisOfDay(0))
$!nextDay.addDays(1)
Tomorrow is $nextDay!

## Example 5
#set($formatterRead = $xwiki.jodatime.getDateTimeFormatterForPattern("yyyy-MM-dd'T'HH:mm:ss+0000"))
#set($formatterWrite = $xwiki.jodatime.getDateTimeFormatterForPattern("MMMM dd 'at' HH:mm"))
#set($postDate = $formatterRead.parseDateTime('2013-02-05T16:23:42+0000'))
$formatterWrite.print($postDate)  ## February 05 at 16:23

Parsing date-time from string and comparing with current date-time

{{velocity}}
#set($strDateTime = "2010-03-12 14:16:31.0")
#set($formatter = $xwiki.jodatime.getDateTimeFormatterForPattern("yyyy-MM-dd HH:mm:ss.S"))
#set($timestamp = $formatter.parseMillis($strDateTime))
#set($datetime = $formatter.parseDateTime($strDateTime))
#if($timestamp == $datetime.millis)
This should always be true!
#end
#set($now = $util.date.time)
#if($timestamp < $now)
In the past!
#elseif($timestamp == $now)
Present!
#else
In the future!
#end
{{/velocity}}

Result

First day of the current week is Monday, November 19, 2007

Last day of the current week is Sunday, November 25, 2007

Document created on 21/11/2007 at 02:19

4 days left till my birthday!

Prerequisites & Installation Instructions

To use, add the com.xpn.xwiki.plugin.jodatime.JodaTimePlugin plugin definition to the xwiki.plugins property in your xwiki.cfg file (this definition is already there if you use XWiki Enterprise 1.2 Milestone 2 or above).

Tags: development
    

Get Connected