Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SimpleDateFormat question 1

Status
Not open for further replies.

advait75

Programmer
Oct 5, 2002
48
IN
Hi,
I have a string like "01-Feb-2004". I need to get it into a 2004-02-01 format so that I can insert into a MYSQL db.
I tried using the SimpleDateFormat, but I get an error saying - Unparseable format.
This is my code-
String todate=request.getParameter("todate");
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
df.setLenient( false );
java.util.Date tDate=null;
tDate = df.parse(todate);

Can someone please help.
Thanks.
 
Almost there, but you need "MMM", rather than "MM" to parse the first string.
Read the docs : they have some example formats :
Code:
String todate= "2000-Feb-01";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MMM-dd");
df.setLenient( false );
java.util.Date tDate=null;
tDate = df.parse(todate);
System.out.println(tDate);
df = new SimpleDateFormat("yyyy-MM-dd");
String newdatestring = df.format(tDate);
System.out.println(newdatestring);

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
lol, yeah, for the first format, not the second though.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top