This is a more complex version of Janne's original CurrentTimePlugin. I had a problem with the original where my server was on the West Coast and I needed the current time on the East Coast. So, I altered the plugin to support timezones, chosing date and/or time display, and the level of detail in what was reported. This plugin is not a drop-in replacement for the original, since I had to abandon the SimpleTimeFormat class that the original used internally.

This plugin was developed under JSPWiki 2.1.85 CVS/JDK1.4.2, and I haven't tried it anywhere else. Let me know if I should make any changes for compatability reasons.

Usage:

   [{INSERT net.colbadhombre.JSPWiki.plugins.CurrentTimeDatePlugin WHERE timezone='US/Samoa' content='date' format='medium'}]

Parameters:

timezone (optional)Any of the strings that java.util.TimeZone recognizes. Skipping thisparameter will use the default time zone at the server.
content (optional)One of "time", "date" or "time and date". Skipping this will return time and date.
format (optional)One of "short", "medium". "long", or "full". Defaults to full.

The source code is below and a JAR is attached at the bottom. The JAR also contains the full source, including test code. The source has no copyright. Feel free to use it whereever and however you'd like.

-- Bradford Holcombe


Source:
package net.colbadhombre.JSPWiki.plugins;

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

import java.text.DateFormat;
import java.util.Date;
import java.util.Map;
import java.util.TimeZone;

public class CurrentTimeDatePlugin implements WikiPlugin
{
   public static final String TIME = "time";
   public static final String DATE = "date";
   public static final String TIME_AND_DATE = "time and date"; // default

   public static final String SHORT = "short";
   public static final String MEDIUM = "medium";
   public static final String LONG = "long";
   public static final String FULL = "full"; // default

   public String execute(WikiContext context, Map params) throws PluginException
   {
      String timezoneString = (String)params.get("timezone");
      String contentString = (String)params.get("content");
      String formatString = (String)params.get("format");

      int format = DateFormat.FULL;
      if (formatString != null)
      {
         if (formatString.equals(SHORT))
         {
            format = DateFormat.SHORT;
         }
         else if (formatString.equals(MEDIUM))
         {
            format = DateFormat.MEDIUM;
         }
         else if (formatString.equals(LONG))
         {
            format = DateFormat.LONG;
         }
      }
      DateFormat timeFormat = DateFormat.getTimeInstance(format);
      DateFormat dateFormat = DateFormat.getDateInstance(format);

      if (timezoneString != null)
      {
         TimeZone zone = TimeZone.getTimeZone(timezoneString);
         if (zone != null)
         {
            timeFormat.setTimeZone(zone);
            dateFormat.setTimeZone(zone);
         }
      }

      Date d = new Date();

      if (contentString != null)
      {
         if (contentString.equals(TIME))
         {
            return timeFormat.format(d);
         }
         else if (contentString.equals(DATE))
         {
            return dateFormat.format(d);
         }
      }

      return timeFormat.format(d) + " " + dateFormat.format(d);
   }
}


ContributedPlugins

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
CurrentTimeDatePlugin.jar 4.6 kB 1 20-Nov-2003 00:14 204.192.50.216
« This page (revision-6) was last changed on 01-Jun-2007 19:29 by mike woodman