This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes SimpleDateFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "SimpleDateFormat" Watch "SimpleDateFormat" New topic
Author

SimpleDateFormat

Alik Elzin
Greenhorn

Joined: Sep 19, 2002
Posts: 15
I want ot create a new instance of SimpleDateFormat with a pattern, which the user of the program gave. How do I know the pattern, given by the user, is a valid one ?
Thanks.
Alik
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Are you not able to pre-define the pattern, so that the user only have the 'right' ones to choose from?
Rene


Regards, Rene Larsen
Dropbox Invite
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1121

Hi Alik,
Unless I am misinterpreting your question, what (I think) you need is the "applyPattern()" method (of class "java.text.SimpleDateFormat"). It throws an exception if the supplied pattern is invalid. Please check the javadocs for more details.
Hope this has helped you.
Good Luck,
Avi.
Alik Elzin
Greenhorn

Joined: Sep 19, 2002
Posts: 15
Hi Rene,
thanks for the advise, but I want to give the user a free hand to choose his format.
Alik Elzin
Greenhorn

Joined: Sep 19, 2002
Posts: 15
Hi Avi,
I looked at the javadocs on java.text.SimpleDateFormat.applyPattern() and the method is not throwing an exception.
Thanks for the effort.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

In the javadoc for SDK 1.4.x it throws an exception.
Rene
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Alik - look again. It throws IllegalArgumentException. Since this is a subclass of RuntimeException it's not required to be declared in a throws clause - but the javadoc does document it anyway, using the @exception tag.
[ January 01, 2003: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Alik Elzin
Greenhorn

Joined: Sep 19, 2002
Posts: 15
Sorry for not writting this earlier, but I'm using JDK 1.3.1.
The method applyPattern() in JDK 1.3.1 does not throw an exception.
Can you give a way to valid the pattern in JDK 1.3.1 ?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: SimpleDateFormat
 
Similar Threads
DateFormat getPattern
DateFormat usage
Date Format Quickie
Calender class doubt
SimpleDateFormat