From 20f41d08a5e4a52a7c50ea2b8eaaf3b0157a35c5 Mon Sep 17 00:00:00 2001 From: mdb Date: Wed, 2 Jul 2003 21:32:27 +0000 Subject: [PATCH] Added support for date formatting. git-svn-id: https://samskivert.googlecode.com/svn/trunk@1157 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../com/samskivert/velocity/I18nTool.java | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/projects/samskivert/src/java/com/samskivert/velocity/I18nTool.java b/projects/samskivert/src/java/com/samskivert/velocity/I18nTool.java index 33a558af..85284c54 100644 --- a/projects/samskivert/src/java/com/samskivert/velocity/I18nTool.java +++ b/projects/samskivert/src/java/com/samskivert/velocity/I18nTool.java @@ -1,5 +1,5 @@ // -// $Id: I18nTool.java,v 1.1 2002/05/02 05:30:31 mdb Exp $ +// $Id: I18nTool.java,v 1.2 2003/07/02 21:32:27 mdb Exp $ // // samskivert library - useful routines for java programs // Copyright (C) 2001 Michael Bayne @@ -20,9 +20,12 @@ package com.samskivert.velocity; +import java.text.SimpleDateFormat; +import java.util.Date; import javax.servlet.http.HttpServletRequest; import com.samskivert.servlet.MessageManager; +import com.samskivert.util.StringUtil; /** * Provides access to a set of application messages (translation strings) @@ -93,6 +96,28 @@ public class I18nTool new Object[] { arg1, arg2, arg3 }); } + /** + * Uses {@link SimpleDateFormat} to translate the supplied {@link + * Long} or {@link Date} argument into a formatted date string using + * the locale appropriate to the current request. + */ + public String date (String format, Object arg) + { + Date when = null; + if (arg instanceof Long) { + when = new Date(((Long)arg).longValue()); + } else if (arg instanceof Date) { + when = (Date)arg; + } else { + System.err.println("Date provided with invalid argument " + + "[fmt=" + format + ", arg=" + arg + ", aclass=" + + StringUtil.shortClassName(arg) + "]."); + return format; + } + SimpleDateFormat fmt = new SimpleDateFormat(format, _req.getLocale()); + return fmt.format(when); + } + /** The servlet request was are providing i18n messages for. */ protected HttpServletRequest _req;