Description#

A tiny plugin displaying the difference in days/hours/minutes/seconds from the current time to a given date.

Installation#

  1. Copy deltatime.jar(info) to the lib folder of your JSPWiki installation (for example to the folder $TOMCAT_HOME/webapps/JSPWiki/WEB-INF/lib/).
  2. Add the package de.jave.jspwiki.plugins to the plugin searchpath in the file WEB_INF/jspwiki.properties (for example jspwiki.plugin.searchPath = de.jave.jspwiki.plugins).
  3. Shutdown and restart tomcat

Example usage#

[{DeltaTimePlugin startDate='2004.01.01 00:00:00'}]

will produce an output like this:

20days 17hours 55minutes 55seconds

Limitations#

  • There is no way to specify the output format/language

Source code#

package de.jave.jspwiki.plugins;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import com.ecyrd.jspwiki.WikiContext;
import com.ecyrd.jspwiki.plugin.PluginException;
import com.ecyrd.jspwiki.plugin.WikiPlugin;

/**
 * @author Markus Gebhard
 */
public class DeltaTimePlugin implements WikiPlugin {
  private static final String PARAMETER_START_DATE = "startDate";

  public String execute(WikiContext context, Map params) throws PluginException {
    Object value = params.get(PARAMETER_START_DATE);
    if (value == null) {
      throw new PluginException("Required parameter '" + PARAMETER_START_DATE + "' missing.");
    }
    String startDateString = (String) value;

    String DATE_FORMAT = "yyyy.MM.dd HH:mm:ss";
    SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
    Date startDate = null;
    try {
      startDate = dateFormat.parse(startDateString);
    }
    catch (ParseException exception) {
      throw new PluginException(
        "Illegal format for parameter " + PARAMETER_START_DATE + ". Expected format: '" + DATE_FORMAT + "'",
        exception);
    }

    Date currentDate = Calendar.getInstance().getTime();

    StringBuffer result = new StringBuffer();
    if (currentDate.before(startDate)) {
      result.append("-");
    }

    long deltaMillis = Math.abs(currentDate.getTime() - startDate.getTime());

    long deltaSeconds = deltaMillis / 1000;
    long deltaMinutes = deltaSeconds / 60;
    long deltaHours = deltaMinutes / 60;
    long deltaDays = deltaHours / 24;

    int days = (int) deltaDays;
    int hours = (int) deltaHours % 24;
    int minutes = (int) deltaMinutes % 60;
    int seconds = (int) deltaSeconds % 60;

    boolean started = false;
    if (started || days > 0) {
      String daysString = days > 1 ? "days" : "day";
      result.append(days + daysString + " ");
      started = true;
    }
    if (started || hours > 0) {
      String hoursString = hours > 1 ? "hours" : "hour";
      result.append(hours + hoursString + " ");
      started = true;
    }
    if (started || minutes > 0) {
      String minutesString = minutes > 1 ? "minutes" : "minute";
      result.append(minutes + minutesString + " ");
      started = true;
    }
    if (started || seconds > 0) {
      String secondsString = seconds > 1 ? "seconds" : "second";
      result.append(seconds + secondsString);
      started = true;
    }
    return result.toString();
  }
}

-- MarkusGebhard, 21-Jan-2004.


FYI - it's 0 minutes not minute

It should be easy to fix - I'll post it when I have a mo.

--Martin F, 02-Mar-2009 19:35

Add new attachment

Only authorized users are allowed to upload new attachments.

List of attachments

Kind Attachment Name Size Version Date Modified Author Change note
jar
deltatime.jar 2.0 kB 1 21-Jan-2004 18:47 MarkusGebhard
« This page (revision-7) was last changed on 02-Mar-2009 19:35 by Martin F