This discussion is archived
8 Replies Latest reply: Apr 20, 2013 2:44 PM by rp0428 RSS

How to convert String into Date

958133 Newbie
Currently Being Moderated
Hi experts,

I have date input as string in format: Feb 02, 2013.
I want to convert this string to date in format : February 02, 2013.

Please help.
  • 1. Re: How to convert String into Date
    gimbal2 Guru
    Currently Being Moderated
    Normally I'd recommend SimpleDateFormat for any kind of date conversion work, but forget that this is a date in this case. You simply want to replace 'string X' with 'string Y' where 'X' in this case is the first three characters of your input string. When you think of it like that, does that make the problem easier for you to answer yourself?
  • 2. Re: How to convert String into Date
    958133 Newbie
    Currently Being Moderated
    Correct me if i am wrong ,if i just replace String, i will get output as string type . My requirement is output should be of Date Type not a string.
  • 3. Re: How to convert String into Date
    952690 Newbie
    Currently Being Moderated
    You can use regular expression API of Java with Enum of month to get the replacement String and then replace it using " String java.lang.String.replaceFirst(String regex, String replacement)" function.

    Sample code for match:
    enum Month
    {
    January, February, March, April, May, June, July, August, September, October, November, December;

    public static String match(String month){
    Month[] months= values();

    Pattern pattern= Pattern.compile(month,Pattern.CASE_INSENSITIVE);

    for(Month m :months){
    Matcher match= pattern.matcher(m.toString());
    if(match.find() && match.start()==0)
    return m.toString();
    }

    return null;
    }
    };
  • 4. Re: How to convert String into Date
    EJP Guru
    Currently Being Moderated
    955130 wrote:
    Correct me if i am wrong ,if i just replace String, i will get output as string type . My requirement is output should be of Date Type not a string.
    If you want a Date type, why are you confusing the issue by specifying a string format? A Date has a large number of string representations. If you just want a Date, parse it as gimbal2 suggested.
  • 5. Re: How to convert String into Date
    958133 Newbie
    Currently Being Moderated
    My approach is :

    Date date_temp = null;
              String d_temp = null;

    String depDate="Feb 02, 2013"; //(Input date)
              SimpleDateFormat dateFormat = new SimpleDateFormat("MMMM dd, yyyy");
    date_temp = (Date) dateFormat.parse(depDate);
              //Value of Date date_temp::::Sat Feb 02 00:00:00 IST 2013
              d_temp = dateFormat.format(date_temp);
              //Value of string d_temp:::February 02, 2013


    I want value of date_temp to be as February 02, 2013.
  • 6. Re: How to convert String into Date
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    955130 wrote at 27.12.2012 22:20:
    [...] My requirement is output should be of Date Type not a string.
    955130 wrote at 28.12.2012 02:01:I want value of date_temp to be as February 02, 2013.
    <i>"February 02, 2013"</i> is the String representation of a <tt>Date</tt> according to the default <tt>Locale</tt> of your java installation.
    So please solve this contradiction: do you need to convert a <tt>String</tt> into a <tt>Date</tt> or into another <tt>String</tt>?

    If it is the former you already solved your problem by line 6 of your code example:
    Date date_temp = (Date) dateFormat.parse(depDate);
    Your line 8 does the conversion of the <tt>Date</tt> back to a <tt>String</tt>.

    bye
    TPD
  • 7. Re: How to convert String into Date
    1004352 Newbie
    Currently Being Moderated
    select to_char(to_date('Feb 02, 2013','Mon dd, yyyy'),'Month dd, yyyy') from dual;
  • 8. Re: How to convert String into Date
    rp0428 Guru
    Currently Being Moderated
    Welcome to the forum!
    >
    select to_char(to_date('Feb 02, 2013','Mon dd, yyyy'),'Month dd, yyyy') from dual;
    >
    You do realize that this is a Java forum and a Java question don't you?

    Java doesn't have anything called 'dual' and doesn't execute database queries.

    It's good that you want to start contributing to the forums but you need to provide suggestions that are appropriate to the forum you post to.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points