File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes  String date in to Date type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark " String date in to Date type" Watch " String date in to Date type" New topic

String date in to Date type


Joined: May 21, 2004
Posts: 10
Im taking the date as dd/MM/yyyy from the user.I want to save it in the database which holds dates in Dste type.
Part of the codings Ive used are given below.

String TodaysDate = "12/30/2003";
SimpleDateFormat formatter= new SimpleDateFormat ("dd/MM/yyyy");

Date today = formatter.parse(TodaysDate);

and so on..

But this doesnt work.
Anyone knows how to convert string date to Date type to save in to a db as well as give the date to the user when required in string format.

Pls help me.
Thanks in advanced for any help...

[ May 25, 2004: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

Not a JSP question, so I'm moving this along to the Java in General(intermediate) forum.

[Asking smart questions] [About Bear] [Books by Bear]
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
When you say doesn't work what do you mean if you can provide that you might get more of a response?

You do know that the format you are giving is for month day year, but yet your string appears to be day month year. Date formatter is the way to do this in general though.
Anurag Mishra
Ranch Hand

Joined: Jun 16, 2003
Posts: 121
Hi Jayanath,

Please check this java script whihc you cann add in your any .js file and use it properly, since i have this so I am pasting it here, this method will take date string in mm/dd/yyyy format and return you the Date object.

which you can store in ddatabase, please tell me if you need anything else.

SCJP 1.2
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
String TodaysDate = "12/30/2003";
SimpleDateFormat formatter= new SimpleDateFormat ("dd/MM/yyyy");

Your date is in the mm/dd/yy format and you are trying to parse it using dd/mm/yy format. 12 is month,30 is day and 2003 is year. so Use following code to parse this date.

I agree. Here's the link:
subject: String date in to Date type
It's not a secret anymore!