Results 1 to 3 of 3
  1. #1
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default 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:

    Java 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.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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;
    }

  3. #3
    javaplus is offline Member
    Join Date
    Nov 2007
    Posts
    97
    Rep Power
    0

    Default

    Thanks hardwired. I just tested it and it works fine. New to me was:

    Java Code:
    DateFormat df = new SimpleDateFormat("yyyy-MMM-dd");
    Thanks once again.

Similar Threads

  1. bytearray to string conversion
    By mew in forum New To Java
    Replies: 1
    Last Post: 01-28-2008, 10:39 AM
  2. Method for String to int conversion
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-02-2008, 06:35 PM
  3. String to Integer conversion
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-17-2007, 03:59 PM
  4. Converting Epoch to string Date
    By amyedwards in forum New To Java
    Replies: 3
    Last Post: 12-14-2007, 10:33 PM
  5. Date as String to TimeStamp
    By ironballs in forum Advanced Java
    Replies: 1
    Last Post: 08-01-2007, 01:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •