DATE problem.

Reply

Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

DATE problem.

 
0
  #1
Mar 13th, 2008
Hi all,

I would like to create a date object and specify a value. I am facing some defficulties.

Here's what I tried:

  1. String dateString = "2005-04-29 00:00:00.0";
  2. Date asOfDate = new Date(dateString); // this gives Illegal argument exception.

I know that Date constructor with String as argument -- as I am using above -- is deprecated. Can someone tell me how I can achieve the same requirement without using the deprecated method, as above?

Please advise...

Thanks.
Last edited by new_2_java; Mar 13th, 2008 at 5:46 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,439
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: DATE problem.

 
0
  #2
Mar 13th, 2008
You can either use a Calendar and set the day, month, year, time values individually or if you have an existing string date that you need to parse you can create a SimpleDateFormat and use the parse(String) method to convert it to a Date object.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: DATE problem.

 
0
  #3
Mar 13th, 2008
Thanks for quick revert.

I will try with SimpleDateFormat, I will update this thread accordingly.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: DATE problem.

 
0
  #4
Mar 14th, 2008
Hi,

I have tried with SimpleDateFormat as follow:
  1. SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-mm-dd hh:mm:ss" );
  2. Date date = sdf.parse("2005-05-29 00:00:00.0");
  3. System.out.println("Date: " + date.toString());

But the output is not the way I want it, the out is like this:
  1. Date: Sun May 29 00:00:00 EDT 2005

I want the out put to be the same as I specify in my SimpleDateFormat.e.g. (2005-05-29 00:00:00.0)

Can you comment on this, please.

Thanks.
Last edited by new_2_java; Mar 14th, 2008 at 10:40 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,658
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 224
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: DATE problem.

 
1
  #5
Mar 14th, 2008
Then use:
sdf.format(date);

The SimpleDateFormat.format(Date) will take any Date object, no matter how it was created and retutn a String based on the pattern you have set at the SimpleDateFormat object.
Meaning that this:
  1. SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-mm-dd hh:mm:ss" );
  2. Date date = new Date();
  3. System.out.println("Date: " + sdf.format(date) );
will return:
2008-03-14 15:53:45
Last edited by javaAddict; Mar 14th, 2008 at 10:56 am.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 126
Reputation: new_2_java is an unknown quantity at this point 
Solved Threads: 6
new_2_java new_2_java is offline Offline
Junior Poster

Re: DATE problem.

 
0
  #6
Mar 14th, 2008
Thanks man, it worked! :-)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC