By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,541 Members | 1,106 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,541 IT Pros & Developers. It's quick & easy.

Convert a string to a date

P: n/a
Hi

I'm trying to convert a string that already looks like a date to a
date that I can use when I pass it from java to the database. I
receive the date in format yyyy-mm-dd and I need it to be a date
variable in 'mm/dd/yyyy' or 'm/dd/yyyy' format. My code is shown
below. Any suggestions?

code:
String sailYear = (String)sail_date.toString().substring(0,4);
String sailMonth = (String)sail_date.toString().substring(5,7);
String sailDay = (String)sail_date.toString().substring(8,10);
int monthCutter = Integer.parseInt(sailMonth);
String newMonth = Integer.toString(monthCutter);
String sailingDate =
newMonth.concat("/").concat(sailDay).concat("/").concat(sailYear);
java.text.SimpleDateFormat.getInstance().setLenien t(true);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String str = sdf.format(sailingDate);

Error:
java.lang.IllegalArgumentException: Cannot format given Object as a
Date

Thanks!
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
SimpleDateFormat.format() takes an instance of Date, not an instance of
String. You need to create a Calendar instance, use its set() method to set
the date fields, then getTime() to get a Date instance. Then you can
format it.
--
Jim McMaster
mailto: ji**********@comcast.net
"Hector A" <ha****@rccl.com> wrote in message
news:69**************************@posting.google.c om...
Hi

I'm trying to convert a string that already looks like a date to a
date that I can use when I pass it from java to the database. I
receive the date in format yyyy-mm-dd and I need it to be a date
variable in 'mm/dd/yyyy' or 'm/dd/yyyy' format. My code is shown
below. Any suggestions?

code:
String sailYear = (String)sail_date.toString().substring(0,4);
String sailMonth = (String)sail_date.toString().substring(5,7);
String sailDay = (String)sail_date.toString().substring(8,10);
int monthCutter = Integer.parseInt(sailMonth);
String newMonth = Integer.toString(monthCutter);
String sailingDate =
newMonth.concat("/").concat(sailDay).concat("/").concat(sailYear);
java.text.SimpleDateFormat.getInstance().setLenien t(true);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
String str = sdf.format(sailingDate);

Error:
java.lang.IllegalArgumentException: Cannot format given Object as a
Date

Thanks!

Jul 17 '05 #2

P: n/a
Hector A wrote:
Hi

I'm trying to convert a string that already looks like a date to a
date that I can use when I pass it from java to the database. I
receive the date in format yyyy-mm-dd and I need it to be a date
variable in 'mm/dd/yyyy' or 'm/dd/yyyy' format. My code is shown
below. Any suggestions?
I have quite a few actually.

code:
String sailYear = (String)sail_date.toString().substring(0,4);
What type is "sail_date"? I bet it is already a String in which case
the sail_date.toString() is pointless.

String sailMonth = (String)sail_date.toString().substring(5,7);
String sailDay = (String)sail_date.toString().substring(8,10);
int monthCutter = Integer.parseInt(sailMonth);
String newMonth = Integer.toString(monthCutter);
Wow. That was amazing and entirely pointless. You parse an integer
into a String and then called toString on it.

String sailingDate =
newMonth.concat("/").concat(sailDay).concat("/").concat(sailYear);
I've never actually seen anybody use String.concat(). Everybody I know
just uses "+".

BTW, aren't you done now? Don't you have what you wanted in sailingDate?
java.text.SimpleDateFormat.getInstance().setLenien t(true);
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Note that the instance of SimpleDateFormat that you called setLenient()
on is different from the instance you have constructed. So the call to
setLenient() is pointless.
String str = sdf.format(sailingDate);
I'm not certain what you expect to accomplish here. It seems you
already formatted the string the way you wanted it.

Error:
java.lang.IllegalArgumentException: Cannot format given Object as a
Date

Thanks!


Sorry if my comments above seem rather harsh. I do not mean to demean
you. But, after all, you did invite comments. I hope mine will provide
constructive criticism.

HTH,
Ray
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.