You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Andrew Hill <an...@gridnode.com> on 2003/01/17 04:40:39 UTC
RE: [OT] Date format
Your best bet is to use the static factory methods in the DateFormat class.
ie:
Locale locale = getLocale(request); //Use struts Action method to get locale
dateTimeFormat =
ateFormat.getDateTimeInstance( DateFormat.SHORT,DateFormat.SHORT, locale);
dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, locale);
timeFormat = DateFormat.getTimeInstance( DateFormat.SHORT, locale);
Its all in the javadocs you know. :-)
<btw>
In case you were wondering how the java.util.Date.toString() works, heres
its source code:
public String toString() {
DateFormat formatter = null;
if (simpleFormatter != null) {
formatter = (DateFormat)simpleFormatter.get();
}
if (formatter == null) {
/* No cache yet, or cached formatter GC'd */
formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",
Locale.US);
simpleFormatter = new SoftReference(formatter);
}
synchronized (formatter) {
formatter.setTimeZone(TimeZone.getDefault());
return formatter.format(this);
}
}
</btw>
-----Original Message-----
From: Suresh Addagalla [mailto:suresh.addagalla@wipro.com]
Sent: Thursday, 16 January 2003 22:56
To: struts-user@jakarta.apache.org
Subject: Date format
Hi,
Please excuse me for the off-topic question, I need this urgently, so
posting it.
How can I get the current date format? (style: DateFormat.SHORT, locale:
Default)
I used:
SimpleDateFormat formatter = new SimpleDateFormat() ;
String format = formatter.toPattern() ;
It gives:
M/d/yy hh:mm a
But what I want is *only* the date part, excluding the time part of it.
Thanks,
Suresh
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: [OT] Date format
Posted by Mark Lepkowski <Ma...@lmco.com>.
RE: [OT] Date formatWith SimpleDateFormat you specify the format as a String. You surely should be able to display that string.
What I am looking for is how to display the *format*, and not how to
display a date in a given format. (snip)
RE: [OT] Date format
Posted by Andrew Hill <an...@gridnode.com>.
DateFormat is an abstract class of which SimpleDateFormat is a subclass.
When you call getDateInstance() what you get back is actually a
SimpleDateFormat - however I am under the distinct impression this depends
on the locale. Some locales could in theory give back something different
again or if the implementation of the factory methods changes in a future
JVM maybe it wouldnt use SimpleDateFormat?
You could do an instanceof to check if its a SimpleDateFormat and proceed
from there - of course for those locales that dont use SimpleDateFormat then
AFAIK you wont be able to get the pattern - this is because pattern is a
SimpleDateFormat feature and not a DateFormat thing so if it isnt a
SimpleDateFormat (or subclass of SimpleDateFormat) then there isnt a pattern
you can make use of anyway! (I could be wrong on that)
Out of curiosity I tried the following:
Locale[] locales = Locale.getAvailableLocales();
System.out.println("numLocales=" + locales.length);
for(int i=0; i < locales.length; i++)
{
Locale locale = locales[i];
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, locale);
String klass = df.getClass().getName();
if(!("java.text.SimpleDateFormat".equals(klass)))
{
System.out.println("class for locale" + locale + " is " +
df.getClass().getName() );
}
}
It reported 145 available locales on my JVM. ALL of them used
SimpleDateFormat.
(But you cant rely on that - and in general its not considered good practice
to make assumptions about which implementation class you get back for a
factory method that returns a superclass or interface...)
Sorry I cant be of more help :-(
-----Original Message-----
From: Suresh Addagalla [mailto:suresh.addagalla@wipro.com]
Sent: Friday, 17 January 2003 22:42
To: 'Struts Users Mailing List'; andrew.david.hill@gridnode.com
Subject: RE: [OT] Date format
Hi,
What I am looking for is how to display the *format*, and not how to
display a date in a given format. So, there doesn't seem to be anything
provided with the API.
I need a method like "toPattern()" of SimpleDateFormat in the DateFormat
class because getDateInstance() returns me DateFormat and not
SimpleDateFormat.
Suresh
>-----Original Message-----
>From: Andrew Hill [mailto:andrew.david.hill@gridnode.com]
>Sent: Friday, January 17, 2003 9:11 AM
>To: Struts Users Mailing List
>Subject: RE: [OT] Date format
>
>
>Your best bet is to use the static factory methods in the
>DateFormat class.
>
>ie:
>Locale locale = getLocale(request); //Use struts Action method
>to get locale
>
>dateTimeFormat =
>ateFormat.getDateTimeInstance(
>DateFormat.SHORT,DateFormat.SHORT, locale);
>dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, locale);
>timeFormat = DateFormat.getTimeInstance( DateFormat.SHORT, locale);
>
>Its all in the javadocs you know. :-)
>
><btw>
>In case you were wondering how the java.util.Date.toString()
>works, heres
>its source code:
>
>public String toString() {
> DateFormat formatter = null;
> if (simpleFormatter != null) {
> formatter = (DateFormat)simpleFormatter.get();
> }
> if (formatter == null) {
> /* No cache yet, or cached formatter GC'd */
> formatter = new SimpleDateFormat("EEE MMM dd
>HH:mm:ss zzz yyyy",
> Locale.US);
> simpleFormatter = new SoftReference(formatter);
> }
> synchronized (formatter) {
> formatter.setTimeZone(TimeZone.getDefault());
> return formatter.format(this);
> }
> }
></btw>
>
>-----Original Message-----
>From: Suresh Addagalla [mailto:suresh.addagalla@wipro.com]
>Sent: Thursday, 16 January 2003 22:56
>To: struts-user@jakarta.apache.org
>Subject: Date format
>
>
>Hi,
>
>Please excuse me for the off-topic question, I need this urgently, so
>posting it.
>
>How can I get the current date format? (style:
>DateFormat.SHORT, locale:
>Default)
>
>I used:
>
>SimpleDateFormat formatter = new SimpleDateFormat() ;
>String format = formatter.toPatte
rn() ;
>
>It gives:
>
>M/d/yy hh:mm a
>
>But what I want is *only* the date part, excluding the time part of it.
>
>Thanks,
>Suresh
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
RE: [OT] Date format
Posted by Suresh Addagalla <su...@wipro.com>.
Hi,
What I am looking for is how to display the *format*, and not how to
display a date in a given format. So, there doesn't seem to be anything
provided with the API.
I need a method like "toPattern()" of SimpleDateFormat in the DateFormat
class because getDateInstance() returns me DateFormat and not
SimpleDateFormat.
Suresh
>-----Original Message-----
>From: Andrew Hill [mailto:andrew.david.hill@gridnode.com]
>Sent: Friday, January 17, 2003 9:11 AM
>To: Struts Users Mailing List
>Subject: RE: [OT] Date format
>
>
>Your best bet is to use the static factory methods in the
>DateFormat class.
>
>ie:
>Locale locale = getLocale(request); //Use struts Action method
>to get locale
>
>dateTimeFormat =
>ateFormat.getDateTimeInstance(
>DateFormat.SHORT,DateFormat.SHORT, locale);
>dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, locale);
>timeFormat = DateFormat.getTimeInstance( DateFormat.SHORT, locale);
>
>Its all in the javadocs you know. :-)
>
><btw>
>In case you were wondering how the java.util.Date.toString()
>works, heres
>its source code:
>
>public String toString() {
> DateFormat formatter = null;
> if (simpleFormatter != null) {
> formatter = (DateFormat)simpleFormatter.get();
> }
> if (formatter == null) {
> /* No cache yet, or cached formatter GC'd */
> formatter = new SimpleDateFormat("EEE MMM dd
>HH:mm:ss zzz yyyy",
> Locale.US);
> simpleFormatter = new SoftReference(formatter);
> }
> synchronized (formatter) {
> formatter.setTimeZone(TimeZone.getDefault());
> return formatter.format(this);
> }
> }
></btw>
>
>-----Original Message-----
>From: Suresh Addagalla [mailto:suresh.addagalla@wipro.com]
>Sent: Thursday, 16 January 2003 22:56
>To: struts-user@jakarta.apache.org
>Subject: Date format
>
>
>Hi,
>
>Please excuse me for the off-topic question, I need this urgently, so
>posting it.
>
>How can I get the current date format? (style:
>DateFormat.SHORT, locale:
>Default)
>
>I used:
>
>SimpleDateFormat formatter = new SimpleDateFormat() ;
>String format = formatter.toPattern() ;
>
>It gives:
>
>M/d/yy hh:mm a
>
>But what I want is *only* the date part, excluding the time part of it.
>
>Thanks,
>Suresh
RE: [OT] Date format
Posted by Andrew Hill <an...@gridnode.com>.
Ok. This is wierd. I posted this in the morning - now its 11pm and it only
just arrived. Maybe I should have used snail mail!
Ah of course - its Friday. Guess the email server is taking it easy... ;-)
-----Original Message-----
From: Andrew Hill [mailto:andrew.david.hill@gridnode.com]
Sent: Friday, 17 January 2003 11:41
To: Struts Users Mailing List
Subject: RE: [OT] Date format
Your best bet is to use the static factory methods in the DateFormat class.
ie:
Locale locale = getLocale(request); //Use struts Action method to get locale
dateTimeFormat =
ateFormat.getDateTimeInstance( DateFormat.SHORT,DateFormat.SHORT, locale);
dateFormat = DateFormat.getDateInstance( DateFormat.SHORT, locale);
timeFormat = DateFormat.getTimeInstance( DateFormat.SHORT, locale);
Its all in the javadocs you know. :-)
<btw>
In case you were wondering how the java.util.Date.toString() works, heres
its source code:
public String toString() {
DateFormat formatter = null;
if (simpleFormatter != null) {
formatter = (DateFormat)simpleFormatter.get();
}
if (formatter == null) {
/* No cache yet, or cached formatter GC'd */
formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",
Locale.US);
simpleFormatter = new SoftReference(formatter);
}
synchronized (formatter) {
formatter.setTimeZone(TimeZone.getDefault());
return formatter.format(this);
}
}
</btw>
-----Original Message-----
From: Suresh Addagalla [mailto:suresh.addagalla@wipro.com]
Sent: Thursday, 16 January 2003 22:56
To: struts-user@jakarta.apache.org
Subject: Date format
Hi,
Please excuse me for the off-topic question, I need this urgently, so
posting it.
How can I get the current date format? (style: DateFormat.SHORT, locale:
Default)
I used:
SimpleDateFormat formatter = new SimpleDateFormat() ;
String format = formatter.toPattern() ;
It gives:
M/d/yy hh:mm a
But what I want is *only* the date part, excluding the time part of it.
Thanks,
Suresh
--
To unsubscribe, e-mail:
<ma...@jakarta.apache.org>
For additional commands, e-mail:
<ma...@jakarta.apache.org>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>