Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

Date problem

Thread view: 
Simonne De Ryck - 15 Apr 2004 18:55 GMT
Hello,

I have a problem whit formatting my date.  On my screen I have a
formattedtextfield whit a dat in (format: dd/MM/yyyy).  Now in my sql I need
the following format MM/dd/yyyy.  But when I try tot format this I get the
following error:
java.lang.IllegalArgumentException: Cannot format given Object as a Date.
What do I wrong, I use the following code:

String sqlFormat = "MM/dd/yyyy";
SimpleDateFormat formatter = new SimpleDateFormat(sqlFormat);
sqlDatum = formatter.format(VanDatum));

VanDatum is the name of the textfield.

Thanks in advance
Els
Eric - 15 Apr 2004 19:06 GMT
Try formatter.format(VanDatum.getText()));

> Hello,
>
[quoted text clipped - 13 lines]
> Thanks in advance
> Els
Simonne De Ryck - 15 Apr 2004 19:13 GMT
I' m afraid that this doesn't work I still have the same error.

> Try formatter.format(VanDatum.getText()));
>
[quoted text clipped - 16 lines]
> > Thanks in advance
> > Els
Bryce (Work) - 15 Apr 2004 19:49 GMT
>Try formatter.format(VanDatum.getText()));

Nope.
format takes a java.util.Date object.

formatter.parse(VanDatum.getText());

will get you a Date.

So, the OP will need 2 date formatters. One to parse format to a
java.util.Date object, then another to format to required date format.

Example:

String originalDateString = "13/03/2004";
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Date date = formatter.parse(originalDateString);
formatter.applyPattern("MM/dd/yyyy");
String newDateString = formatter.format(date);

>> Hello,
>>
[quoted text clipped - 14 lines]
>> Thanks in advance
>> Els

--
now with more cowbell
Bjorn Abelli - 15 Apr 2004 19:40 GMT
"Simonne De Ryck" wrote...

> I have a problem whit formatting my date.
> On my screen I have a formattedtextfield whit
[quoted text clipped - 9 lines]
> SimpleDateFormat formatter = new SimpleDateFormat(sqlFormat);
> sqlDatum = formatter.format(VanDatum));

It seems you need a two step process:

1. First parse the textstring to a Date

 String form = "dd/MM/yyyy";
 SimpleDateFormat formatter = new SimpleDateFormat(form);
 Date d = formatter.parse(VanDatum.getText());

2. Format a new string from the Date

 form = "MM/dd/yyyy";
 formatter = new SimpleDateFormat(form);
 String sqlDatum = formatter.format(d);

However!

I would suggest that you look further into using PreparedStatement if you're
inserting a Date into a database.

In that way you can skip the second step above and use the Date as ... a
Date.

For further help on Java and databases, you can ask questions in
comp.lang.java.databases.

// Bjorn A
Roedy Green - 15 Apr 2004 22:23 GMT
>SimpleDateFormat formatter = new SimpleDateFormat(sqlFormat);
>sqlDatum = formatter.format(VanDatum));

see http://mindprod.com/jgloss/calendar.html

Look particularly at the SimpleDateFormat.parse method for String ->
Calendar and SimpleDateFormat.format for Calendar -> String.

(The conversions take a few steps).

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.