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

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "date or calendar object creation" Watch "date or calendar object creation" New topic

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

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
I agree. Here's the link: http://aspose.com/file-tools
subject: date or calendar object creation
Similar Threads
How to manipulate date / time using Calendar and then put the modified data inside the Date class?
how to convert gregoriancalendar obj to sqldate?
java.sql.Date math
rendering calendar object
add 10 days to currentDate in java.util.Date or cast to Calendar?