473,769 Members | 4,470 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_da te.toString().s ubstring(0,4);
String sailMonth = (String)sail_da te.toString().s ubstring(5,7);
String sailDay = (String)sail_da te.toString().s ubstring(8,10);
int monthCutter = Integer.parseIn t(sailMonth);
String newMonth = Integer.toStrin g(monthCutter);
String sailingDate =
newMonth.concat ("/").concat(sailD ay).concat("/").concat(sailY ear);
java.text.Simpl eDateFormat.get Instance().setL enient(true);
SimpleDateForma t sdf = new SimpleDateForma t("MM/dd/yyyy");
String str = sdf.format(sail ingDate);

Error:
java.lang.Illeg alArgumentExcep tion: Cannot format given Object as a
Date

Thanks!
Jul 17 '05 #1
2 39807
SimpleDateForma t.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**********@co mcast.net
"Hector A" <ha****@rccl.co m> wrote in message
news:69******** *************** ***@posting.goo gle.com...
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_da te.toString().s ubstring(0,4);
String sailMonth = (String)sail_da te.toString().s ubstring(5,7);
String sailDay = (String)sail_da te.toString().s ubstring(8,10);
int monthCutter = Integer.parseIn t(sailMonth);
String newMonth = Integer.toStrin g(monthCutter);
String sailingDate =
newMonth.concat ("/").concat(sailD ay).concat("/").concat(sailY ear);
java.text.Simpl eDateFormat.get Instance().setL enient(true);
SimpleDateForma t sdf = new SimpleDateForma t("MM/dd/yyyy");
String str = sdf.format(sail ingDate);

Error:
java.lang.Illeg alArgumentExcep tion: 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_da te.toString().s ubstring(0,4);
What type is "sail_date" ? I bet it is already a String in which case
the sail_date.toStr ing() is pointless.

String sailMonth = (String)sail_da te.toString().s ubstring(5,7);
String sailDay = (String)sail_da te.toString().s ubstring(8,10);
int monthCutter = Integer.parseIn t(sailMonth);
String newMonth = Integer.toStrin g(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(sailD ay).concat("/").concat(sailY ear);
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.Simpl eDateFormat.get Instance().setL enient(true);
SimpleDateForma t sdf = new SimpleDateForma t("MM/dd/yyyy");
Note that the instance of SimpleDateForma t that you called setLenient()
on is different from the instance you have constructed. So the call to
setLenient() is pointless.
String str = sdf.format(sail ingDate);
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.Illeg alArgumentExcep tion: 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
9305
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 three dropdow boxes for hours, minutes, and AM/PM. All of these need to be considered together and converted to one Unix Timestamp and then inserted to the MYSQL date field. The type of field is INT (11) so that I can instead of the standard...
2
5089
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 set to "en-UK" and date to
12
3862
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
4606
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 "dd/MM/yyyy" input as "01082003" convert to date value as, 01 Aug 2003 Example, User Defined Date Format as "yyyy,dd,MM"
4
39344
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 will convert each of the records of these service dates to the first date of that month, with results showing: 7/1/2006, 7/1/2006, 7/1/2006. How would you place an expression on a query that will convert any given date to the first day of the month...
6
21206
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
6056
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 app. 3. I need to convert the received date string in to a date w.r.t my local time zone.
4
30266
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 Thanks in advance
4
3020
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 machines. Convert.ToDateTime("") I get the string in the format MM/DD/YYYY (06/26/2007) The code breaks and I get the "String was not recognized as a valid DateTime" error message unless I manually change the input string to
0
9591
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10225
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9867
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3969
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.