aspose file tools*
The moose likes Beginning Java and the fly likes date or calendar object creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "date or calendar object creation" Watch "date or calendar object creation" New topic
Author

date or calendar object creation

Peter Guillebaud
Ranch Hand

Joined: Jul 11, 2001
Posts: 57
hi, how do you make a calendar or a date object from a string variable like "1 Jan 2001"? I want to use this object in a sql query against a microsoft sql database with a datetime column.
many thanks
Peter Guillebaud
Ranch Hand

Joined: Jul 11, 2001
Posts: 57
I try this code
String myDate = "1 Jan 2001";
GregorianCalendar gc = new GregorianCalendar();
DateFormat df = DateFormat.getDateInstance( DateFormat.SHORT );gc.setTime( df.parse( myDate ) );
but it gives exception:
javax.servlet.ServletException: Unparseable date: "1 Jan 2001"
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If you consult the documentation, you will see that DateFormat.SHORT is expected to be completely numeric, so obviously it is not a good candidate here. DateFormat.MEDIUM is more likely to work for you, but it depends on the Local used, and the exact format used is not documented. Your best best is to use SimpleDateFormat instead:
<pre>DateFormat df = new SimpleDateFormat("dd MMM yyyy");</pre>
This allows you to more explicitly control the format used.


"I'm not back." - Bill Harding, Twister
Peter Guillebaud
Ranch Hand

Joined: Jul 11, 2001
Posts: 57
thanks very much Jim
 
wood burning stoves
 
subject: date or calendar object creation