Re: Formatting a date
Alex Twisleton-Wykeham-Fiennes <[email protected]> Tue, 28 Mar 2006 03:15:47 +0100
| Newsgroups | gmane.comp.java.webmacro.user |
|---|---|
| Message-ID | <[email protected]> |
--Boundary-00=_TxJKEEOK+MhQdsj
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
On Mon 27 March 2006 19:12, Keats Kirsch wrote:
> Alex Twisleton-Wykeham-Fiennes wrote:
> >Why not make it so that the wrapper class extends Date so that you can
> > then also used the wrapped class as a drop-in replacement for Date in
> > other methods.
> >
> >Also, why not make the wrapper include a hashtable accessor that enables
> > you to extend the number of registered formats on it?
>
> Exactly what I was thinking. Don't know when I'll find the time, but it
> should be a fun little project.
the attached should be roughly what you are looking for. I'm not sure what
the correct way of wrapping it up should be to drop it into a context though
as I have my own framework of utilities and I don't use the conventional
context tools that much.
Alex
--Boundary-00=_TxJKEEOK+MhQdsj
Content-Type: text/x-java;
charset="iso-8859-1";
name="DateFactory.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
filename="DateFactory.java"
import java.util.*;
import java.text.*;
public class DateFactory
{
private final Map __simpleDateFormats = new HashMap();
public void registerFormat(String name,
SimpleDateFormat sdf)
{
synchronized (__simpleDateFormats) {
__simpleDateFormats.put(name, sdf);
}
}
public void registerFormat(String name,
String format)
{
registerFormat(name, new SimpleDateFormat(format));
}
public SimpleDateFormat getFormat(String name)
{
synchronized (__simpleDateFormats) {
return (SimpleDateFormat) __simpleDateFormats.get(name);
}
}
public DateWrapper parse(String date,
String formatName)
throws ParseException
{
SimpleDateFormat sdf = getFormat(formatName);
return new DateWrapper(sdf.parse(date));
}
public DateWrapper wrap(Date date)
{
return new DateWrapper(date);
}
public class DateWrapper
extends Date
{
private DateWrapper(Date date)
{
super(date.getTime());
}
public Object get(Object key)
{
return getFormat(key.toString()).format(this);
}
}
}
--Boundary-00=_TxJKEEOK+MhQdsj--
-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642