473,386 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Convert a string to a date

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
2 39763
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: perplexed | last post by:
How do you convert a user inputted date to a unix timestamp before insterting it into your database? I have a form, with a textfield for a date that the user inputs in the format mm-dd-yyyy and...
2
by: Franck | last post by:
Hi, 'm gettin mad about date conversion. Here is the point. Got and add-in for Excel which call functions from a web service (on a remote server) The remote server has regional settings...
12
by: DC Gringo | last post by:
How can I convert this pubLatest to a date with format "m/d/yyyy"? Dim pubLatest As New Date pubLatest = Me.SqlSelectCommand1.Parameters("@pubLatest").Value -- _____ DC G
1
by: abcabcabc | last post by:
I write an application which can let user define own date format to input, How to convert the date string to date value with end-user defined date format? Example, User Defined Date Format as...
4
by: perryclisbee via AccessMonster.com | last post by:
I have dates of service for several people that range all over each month. ie: patient had dates of service of: 7/3/2006, 7/24/2006 and 7/25/2006. I need to create a new field via a query that...
6
by: vunet.us | last post by:
How can I convert UTC time such as 1173451235415 to a local time I can read? Thank you
8
by: deepak_kamath_n | last post by:
Hello, I have the following scenario: 1. My application receives the date from another application as a string 2. The other application is running in a different time zone as compared to my...
4
by: Ashraf Ansari | last post by:
Hi, How Can I convert MM/dd/yyyy format into dd/MM/yyyy and the date which is converted into dd/MM/yyyy should be in DateTime format. I do not want to store it as a string. Please help ...
4
by: =?Utf-8?B?YW5kcmV3?= | last post by:
I am running an ASP.net program written in VB. At one point I try to convert a date string into a date time object... this string is from a central dev server and the code works on many other...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.