Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-05-2007, 09:21 PM
Member
 
Join Date: Nov 2007
Posts: 97
javaplus is on a distinguished road
String to Date conversion
Hi,
I am parsing a text file and have to store the require information into database. The text file is simply an ASCII file. It has date in it which has following format:
YYYY-MON-DD

Example:
2001-jan-12
2006-feb-17

I get date in the form of String and have to store it into Date format. I want a method to be written so I can reuse it later. The sketch of the method is:

Code:
Date strToDate(String date){ ... // code goes here ... Date date = new Date(year,month,day); return date; }
I would be thankful if someone can guide me.

Regards.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-05-2007, 10:00 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
Code:
// If you are certain of the entry format you can try // something like this. Save it as a member variable: DateFormat df = new SimpleDateFormat("yyyy-MMM-dd"); // 2001-jan-12 /** Let it parse your string into a Date. */ Date strToDate(String dateStr) { Date date = null; try { date = df.parse(dateStr); } catch(ParseException e) { // recover } return date; }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-06-2007, 08:16 PM
Member
 
Join Date: Nov 2007
Posts: 97
javaplus is on a distinguished road
Thanks hardwired. I just tested it and it works fine. New to me was:

Code:
DateFormat df = new SimpleDateFormat("yyyy-MMM-dd");
Thanks once again.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
bytearray to string conversion mew New To Java 1 01-28-2008 11:39 AM
Method for String to int conversion Java Tip Java Tips 0 01-02-2008 07:35 PM
String to Integer conversion eva New To Java 2 12-17-2007 04:59 PM
Converting Epoch to string Date amyedwards New To Java 3 12-14-2007 11:33 PM
Date as String to TimeStamp ironballs Advanced Java 1 08-01-2007 02:43 PM


All times are GMT +3. The time now is 06:24 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org