Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert String to Date

 
Andrea Corbally
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to java, but familar enough with programming, I thought I should post in intermediate, I hope someone here can help. My question is how to convert a string to a date. I am retreiving a month, from a drop down that I have created full with all 12 months. This is returned as a string. I need this with another text field where they enter the four digit year to turn into a date field. First with the first of the month and then with the last day of the month. Example
Drop down selection: "March"
Text entry for year: "2002"
I need it to look like this 01-MAR-2003 and 31-MAR-2003 in order for me to insert it into the database.
Let me know if you can help.
Thanks.
 
a young
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you elaborate a bit? It looks like you just want to convert a few strings to a single string in a certain format. Is that correct?
 
Andrea Corbally
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I believe that is what I want to do. I have an array of months that I need to turn into the date field (first day of the month and last day of the month) based upon the month they select.
Ex:
They choose April from the drop down. I want that to convert to 01-APR-2003
Does that make sense? It is a string and has no date value. I need to determine what month they choose and add the day and year to it.
 
a young
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I'm still not sure about a few things but I wrote up some sample code that may help:

This code assumes you're using JTextBox and JComboBox to get the information. I left the date out because I wasn't sure how you were retrieving it.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a couple of Date Utility methods laying around here, they should help you out:

to parse the date, you could use the SimpleDateFormat:

Jamie
[ August 05, 2003: Message edited by: Jamie Robertson ]
 
Andrea Corbally
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all of your comments. I have gotten my string down to "-MAR-2003". I now need to convert it into a date and add the day. So it would look like "01-MAR-2003" and "31-MAR-2003". Any ideas on how I can do that?
 
a young
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post some code snipets or something? Your requests still have me confused.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic