Extensions Wiki » Extensions » Joda Time Plugin

Joda Time Plugin

Last modified by Marius Dumitru Florea on 2013/04/15 16:13
cogProvides a Velocity-friendly API on top of the Joda Time library
TypePlugin
Developed by

XWiki Development Team

LicenseGNU Lesser General Public License 2.1
Bundled With

XWiki Enterprise, XWiki Enterprise Manager

Download

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);

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

Follow these steps:

  • Add the JAR in your container classpath (WEB-INF/lib)
  • Edit xwiki.cfg and add the following line to the list of plugins :
    xwiki.plugins=\
          [...]
           ... ,\
         <plugin package>
  • Restart your container
  • Verify the plugin is properly installed by typing the following in a wiki page :
    {{velocity}}
    $xwiki.<plugin name>.name
    {{/velocity}}

    If the installation has been successful, you will see <plugin name>.


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
Created by Thomas Mortagne on 2007/12/10 16:11

Download XWiki