| | |
DATE problem.
![]() |
•
•
Join Date: Apr 2007
Posts: 126
Reputation:
Solved Threads: 6
Hi all,
I would like to create a date object and specify a value. I am facing some defficulties.
Here's what I tried:
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.
I would like to create a date object and specify a value. I am facing some defficulties.
Here's what I tried:
Java Syntax (Toggle Plain Text)
String dateString = "2005-04-29 00:00:00.0"; 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.
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.
•
•
Join Date: Apr 2007
Posts: 126
Reputation:
Solved Threads: 6
Hi,
I have tried with SimpleDateFormat as follow:
But the output is not the way I want it, the out is like this:
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.
I have tried with SimpleDateFormat as follow:
Java Syntax (Toggle Plain Text)
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-mm-dd hh:mm:ss" ); Date date = sdf.parse("2005-05-29 00:00:00.0"); System.out.println("Date: " + date.toString());
But the output is not the way I want it, the out is like this:
Java Syntax (Toggle Plain Text)
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.
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:
will return:
2008-03-14 15:53:45
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:
Java Syntax (Toggle Plain Text)
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-mm-dd hh:mm:ss" ); Date date = new Date(); System.out.println("Date: " + sdf.format(date) );
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
![]() |
Similar Threads
- date problem (Visual Basic 4 / 5 / 6)
- Small date problem (ASP)
- Dreamweaver MX2004 & date problem, etc. (Graphics and Multimedia)
- Access Date Problem (Computer Science)
- Hi Access Date Problem (> Now +7 Days And < Now +2 Months) (Computer Science)
- Date Problem (JSP)
Other Threads in the Java Forum
- Previous Thread: multi-client server [problem with code - please have a look]
- Next Thread: NetBeans
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows






