Problem in  Converting String to Date

Hi All,
I am having one String
String date = "2006-01-17 15:19:57.0"
I want to parse this String into Date object.
I will really appriciate if somebody helps me out.
Thanks.

You're specifying a 'T' and a timezone in your format, but they're not present in the string you're parsing.
I'm assuming from the way you're printing out the date, that your thinking is along these lines: "sdfInput will parse the input string, no matter what format it's in, and will produce a Date object. That Date object wil have the format specified in sdfInput."
This is wrong on a couple of fronts:
1) DateFormat doesn't magically figure out what format it's supposed to use for the String it's parse()ing. The String has to match the DF's format.
2) Dates don't have formats. Only Strings do. A Date object is just a long. There's no relationship whatsoever between the Date that you get from parse() and the format that was used to produce it. When you print out a Date as you're doing, its toString method is called, which in turn uses a default format for your Locale.
If you want to turn a date string in one format into a date string in another format, use two different DateFormat objects with two different formats. Date date = df1.parse(inputString);
String outputString = df2.format(date);

Similar Messages

  • Facing problem in converting string to date using getOANLSServices()

    I am trying to set a value for date field in my vo and trying to insert into the table.
    In controller I am getting the String which has a date:
    ex: String date="01-NOV-2007";
    while setting into the row I need to convert into Date but it is returning null.
    The below code I used
    to convert into date :
    Date dt = new Date(getOADBTransaction().getOANLSServices().stringToDate(date));
    But this dt is returning a null value is there any solution please advise me.
    Regards!
    Smarajeet

    Smarajeet ,
    See this thread, in one of my replies i have mentioned how to convert string to java.sql.date.You can use the same for oracle.jbo.domain.Date.
    urgent!How to set the default selected date for an OAMessageDateFieldBean
    --Mukul                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Convert String to Date and Format the Date Expression in SSRS

    Hi,
    I have a parameter used to select a month and year  string that looks like:    jun-2013
    I can convert it to a date, but what I want to do is,  when a user selects a particular month-year  (let's say "jun-2013")
    I  populate one text box with the date the user selected , and (the challenge Im having is)  I want to populate a text box next to the first text box with the month-year  2 months ahead.    So if the user selects 
    jun-2013   textbox A will show  jun-2013 
    and textbox B will show  aug-2013..
    I have tried:
    =Format(Format(CDate(Parameters!month.Value  ),  
    "MM-YYYY"  )+ 2  )   -- But this gives an error
    This returns the month in number format   like "8"    for august...
    =Format(Format(CDate(Parameters!month.Value  ), 
    "MM"  )+ 2  )
    What is the proper syntax to give me the result    in this format =  "aug-2013"  ???
    Thanks in advance.
    MC
    M Collier

    You can convert a string that represents a date to a date object using the util.scand JavaScript method, and then format a date object to a string representation using the util.printd method. For more information, see:
    http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/JS_API_AcroJS.88.1254.html
    http://livedocs.adobe.com/acrobat_sdk/9.1/Acrobat9_1_HTMLHelp/JS_API_AcroJS.88.1251.html
    In your case, the code would be something like:
    var sDate = "2013-01-10";
    // Convert string to date
    var oDate = util.scand("yyyy-mm-dd", sDate);
    // Convert date to new string
    var sDate2 = util.printd("mm/dd/yyyy", oDate);
    // Set a field value
    getField("date2").value = sDate2;
    The exact code you'd use depends on where you place the script and where you're getting the original date string, but this should get you started.

  • I want convert string to date format in Oracle

    Dear All
    I want convert string to date format in Oracle,Format is given below
    'Friday, 02 March 2012 2:44 PM' to '02/03/2012 2:44 PM'

    >
    Hi Parwez,
    I want convert string to date format in Oracle,Format is given below
    'Friday, 02 March 2012 2:44 PM' to '02/03/2012 2:44 PM'SELECT TO_DATE('Friday, 02 March 2012 2:44 P.M.', 'DAY, DD MONTH YYYY HH:MI A.M.') from dual;
    As well as what the other poster suggested, look here: http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34924
    HTH,
    Paul...

  • Converting String to Date

    Hi All,
    I am having a String value which is having date in the format 'Mon Apr 14 16:07:52 IST 2008'
    Now I want to convert this String to Date with format 'mm/dd/yyyy' , to display as 04/14/2008
    Also I want to add 5 days to this date and display the new date 04/19/2008
    I don't know How to convert the string which is having date in 'Mon Apr 14 16:07:52 IST 2008' format to format I need.
    Please help me.

    Hi ,
    I wrote a class that tries to parse a String Date and converts it to object. However, I found a very interesting thing while testing this code. I was trying to paarse "2008:03:04 16:21:05" string by using
    DateFormat.getDateFromString(null,"2008:03:04 16:21:05" )
    It should by pass all try loop accept the one with yyyy:MM:dd HH:mm:ss format. But it went inside the try loop which had MM:dd:yyyy HH:mm:ss format and gave me a wrong date object with year 0171, april 03.
    Could any one help me to understand why it is happening? as a work around I put MM:dd:yyyy HH:mm:ss at the end of the program and it is giving me correct object as it did not have to go that far before matching the format.
    Thanks,
    AL-Amin
    Code:
    import java.text.*;
    import java.util.*;
    public class DateFormat
    public Date getDateFromString(String format, String parseDate)
         Date d;
         if(format!=null) {
    try
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    d = sdf.parse(parseDate);
    catch(Exception e)
    System.out.println(e.toString());
    d = null;
    return d;
         else {
              try
                   format = "yyyy-MM-dd HH mm ss";
         SimpleDateFormat sdf = new SimpleDateFormat(format);
         d = sdf.parse(parseDate);
         catch(Exception e)
         System.out.println(e.toString());
         System.out.println("The Date was not of '"+format+" ' format");
         d = null;
         if(d==null) {
              try
                        format = "yyyy-MM-dd HH:mm:ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
         if(d==null) {
              try
                        format = "yyyy-MM-dd HH_mm_ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
         if(d==null) {
              try
                        format = "yyyy-MM-dd HH-mm-ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
         if(d==null) {
              try
                        format = "yyyy-MM-dd-HH-mm-ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
         if(d==null) {
              try
                        format = "yyyyMMddHHmmss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
         if(d==null) {
              try
                        format = "MM-dd-yyyy HH:mm:ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
              if(d==null) {
              try
                        format = "dd-MM-yyyy HH:mm:ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
         if(d==null) {
              try
                        format = "MM:dd:yyyy HH:mm:ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
         if(d==null) {
              try
                        format = "dd:MM:yyyy HH:mm:ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
    if(d==null) {
              try
                        format = "yyyy:MM:dd HH:mm:ss";
              SimpleDateFormat sdf = new SimpleDateFormat(format);
              d = sdf.parse(parseDate);
              catch(Exception e)
              System.out.println(e.toString());
              System.out.println("The Date was not of '"+format+" ' format");
              d = null;
         if(d==null) {
              System.err.println("None of the Date format worked. Please modify the calling method and provide a specific format");
         return d;
    }

  • How to convert string to date based on regional settings

    How can I convert a simple string ("01/04/2003" or "01-04-2003" or "01 April 2003") to a date formatted according to the regional settings?
    For example, if my regional settings is set to "dd mm yyyy", the result will be 01 as dd, 04 as mm and 2003 as yyyy.
    If the regional settings is set to "mm dd yyyy", the result will be 01 as mm, 04 as dd and 2003 as yyyy
    Hope someone can help.
    Thanks in advance.

    I think you have the wrong idea about how Oracle stores dates. Any date in Oracle is stored in a seven byte representation
    SQL> SELECT DUMP(last_hire_dt) FROM emp_t WHERE rownum = 1;
    DUMP(LAST_HIRE_DT)
    Typ=12 Len=7: 119,191,5,6,1,1,1This storage is independent of the NLS date settings. The TO_CHAR(dt,format) and TO_DATE(str,format) functions serve to translate this internal representation into the external presentation supplied by the format. So, if you need to translate a date that is contained in a string into an Oracle date datatype, you need to tell Oracle how to parse the string.
    If the string format matches your NLS_DATE_FORMAT parameter, Oracle will do this implicitly. Otherwise, you need to tell Oracle explicitly which piece is which. For example, is the string (ignoring the problem of two digit years :-) ):
    '02/01/03'
    Feb. 1, 2003, Jan. 2, 2003, or Mar. 1, 2002? If your date format is dd-MON-yyyy as mine is, then:
    SQL> SELECT TO_DATE('02/01/03') FROM dual;
    SELECT TO_DATE('02/01/03') FROM dual
    ERROR at line 1:
    ORA-01843: not a valid month
    SQL> SELECT TO_DATE('30-Jan-2003') FROM dual;
    TO_DATE('30
    30-JAN-2003HTH
    John

  • Cannot convert string to date

    Hey Guys,
    I'm starting powershell and I'm having a headheck (I'm searching for hours) with a piece of code :)
    I'm trying to do that :
    $LogLines = (Get-Content -Path $LogPath)
    Foreach ($LogLine in $LogLines) {
    $LogSubString = $LogLine.Substring(21,15)
    [datetime]$date = $LogSubString
    But I have an error
    Cannot convert value "‎10‎-‎22‎-‎2014" to type "System.DateTime". Error: "String was not
    recognized as a valid DateTime."
    As you see, the value I read from a file is "10-22-2014".
    When I try this, it is working :
    $LogLines = (Get-Content -Path $LogPath)
    Foreach ($LogLine in $LogLines) {
    $LogSubString = "10-22-2014"
    [datetime]$date = $LogSubString
    So I don't understand why it's not working since "10-22-2014" and $LogLine.Substring(21,15) represents the same value but one is not working.
    Can someone please help me ? :)
    Cheers,
    Jeremie

    Get-Content has a parameter -Encoding UTF-8
    But I not sure that's the way to go, that could result in data loss / confusion
    Or take a good look at the string, leave $LogLine.Substring(21,15)
    as is.
    $LogLines = (Get-Content -Path $LogPath)
    Foreach ($LogLine in $LogLines) {
    $LogSubString = $LogLine.Substring(21,15)
    "LogSubString=$LogSubString"
    "LogSubString.Length=$($LogSubString.Length)"
    FOR ($I=0;$I -LT $LogSubString.Length;$I++)
    $Char = $LogSubString.Chars($I)
    "I=$I Char=$Char Int=$($Char -as [Int])"
    [datetime]$date = $LogSubString

  • Need info on SimpleDateFormat, Converting String to Date

    I'm a newbie and doing a conversion of a string to a date and it's adding a little less than 11 minutes. I know I'm missing something really simple but as I read it I ought to be able to convert "20030125 084539.637696" to Sat Jan 25 08:45:39 not 8:56! Also, for the time being I'm ignoring the zulu 'Z' because I can't find a pattern that'll take it.
    System.out.println("INFO:MetadataExtractorBean::filestarttime:" + filestarttime);
    filestarttime = filestarttime.replace("Z","");
    System.out.println("after filestarttime.replace(Z,null) filestarttime:" + filestarttime);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd hhmmss.SSSSSS");
    Date convertedDate = dateFormat.parse(filestarttime);
    System.out.println("after dateFormat.parse(filestarttime) convertedDate:" + convertedDate);
    INFO:MetadataExtractorBean::filestarttime:20030125 084539.637696Z
    after filestarttime.replace(Z,null) filestarttime:20030125 084539.637696
    after dateFormat.parse(filestarttime) convertedDate:Sat Jan 25 08:56:16 EST 2003
    Can someone help me with a) why it doesn't remain 8:45, and b) how to modify the pattern to be able to parse a zulu date. Thanks in advance.

    import java.text.*;
    public class ParsingExample {
        public static void main(String[] args) throws ParseException {
            String s = "20030125 084539.637";
            SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd HHmmss.SSS");
            System.out.println(f.parse(s));
    }Your round up was because ".637696" was taken to mean 637,696 milliseconds. SimpleDateFormat doesn't have a way to accept microseconds, and anyway, java.util.Date only has millisecond precision. I also changed "hh" to "HH" because I assume this is with a 24 hour clock.
    edit: you can tell your date format not to do these roll ups by setting:
    f.setLenient(false);

  • How to convert String to date ? - MS access

    I want to convert a string "10-10-08" [ MM-dd-yy ] into date type in same format to use that in query statement [MS Access].
    In table , I am using field called "date" with "MM-dd-yy" format.
    Example:
    <%@ page import= "java.sql.*"%>
    <%@ page import="java.text.SimpleDateFormat"%>
    <%@ page import="java.util.Date"%>
    <%
    String d="10-9-08";
         SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yy");
              Date date = dateFormat.parse(d);
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection con = DriverManager.getConnection("jdbc:odbc:bspipdb");
         Statement st = con.createStatement();
    ResultSet rs=st.executeQuery("Select * from datacollectdb where date='"+date+"'");
    %>
    I am getting "java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.
    " error.
    How to use the string "10-10-08" in where statement?

    sangee wrote:
    I want to convert a string "10-10-08" [ MM-dd-yy ] into date type in same format to use that in query statement [MS Access].
    In table , I am using field called "date" with "MM-dd-yy" format.I think a column named "date" is a very bad idea. Not just because "date" is a keyword. It's not very self-describing. How about "birth_date" or "hire_date" or "went_on_my_first_date"?
    Example:Scriptlets? There's your second mistake.
    <%@ page import= "java.sql.*"%>
    <%@ page import="java.text.SimpleDateFormat"%>
    <%@ page import="java.util.Date"%>
    <%
    String d="10-9-08";The way you're doing things, "2-40-99" is a valid date. Is that what you want?
         SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yy");
              Date date = dateFormat.parse(d);
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         Connection con = DriverManager.getConnection("jdbc:odbc:bspipdb");Wrong again. Better to use a PreparedStatement and bind that Date.
         Statement st = con.createStatement();
    ResultSet rs=st.executeQuery("Select * from datacollectdb where date='"+date+"'");
    %>
    I am getting "java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.
    " error.
    How to use the string "10-10-08" in where statement?Use PreparedStatement and bind.
    Your JSP should not be getting a database connection. Pool it.
    Your code is wrong on every level. You don't close resources, you don't separate you app into layers, your database code is not in a POJO. I'd recommend that you throw this away and start again.
    %

  • How to convert string to date

    HI Everyone,
    i am trying to convert a date which is in string format to a date format...but i am having this error
    javax.servlet.ServletException: Unparseable date: "2004-05-03"
    Can anyone help?
    Here is a piece of code :-
    SimpleDateFormat formatter= new SimpleDateFormat ("yyyy-mm-dd");
    Date today = formatter.parse(fulldate);

    You need to change your formatting string to: "yyyy-MM-dd" Lower case 'm' is for minutes...

  • Convert string to date for Ms Access

    I have some date in a textFile!!!
    I get them via Buffered and FileReader into my java application and put them into a Vector!
    Every date is formatted as Strings and I want to convert them into a date format(YYYYMMDD).
    The date are not used for " System.out.println", but I want to insert them into a Access-database(use a jdbc/odbc bridge).
    The Access database seems only to accept YYYY-MM-DD, the only formatting way is how you choose to "show" the dates (short, long etc) but anyway....(:-)
    I�ve tried following example with format.getTime() and it works. But I want to insert the date from
    my textfiles instead.
    Here some examples from my code with the getTime()method!!!
    String str = String.valueOf(fields.get(3));
    String ar = str.substring(0,4)+"-";
    String m = str.substring(4,6)+"-";
    String d = str.substring(6,8);
    String datum = ar+m+d; //this is my date in a String format!!!
    Calendar calendar = new GregorianCalendar();
    Date date = calendar.getTime();//this objectref. takes the sys.date i want my string date instead(:-)
    DateFormat localFormat = DateFormat.getDateInstance();
    DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd" );//the only format for Access I think!
    for (int j=0; j<(fields.size()-1); j++){//pos. from my vector
    sql = sql + "'" + fields.get(j) + "'" + "," ;     
    sql = sql + "'" + format1.format( date ) "'" ; //the last field i the db
    db myTest = new db();
    myTest.executeDML("insert into tblBooks values "+sql+")");     
    Sorry for my "bumpy" English(:-) And thanks! Eva

    If you tell us what the format if the dates in the file is, we might be able to suggest something :)

  • Converting string to date in mm/dd/yyyy format

    Hi,
    I used simpledateformat to convert date to "MM/dd/yyyy" format.
    Now when i want to insert into db, i want the value as date object instead of string [returned by format method of SimpleDateFormat]
    what should i do?
    Date dt = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    System.out.println("Formatted date is coming as "+sdf.format(dt));
    Date dt1 = sdf.parse(sdf.format(dt);
    System.out.println("parsed date is coming as " +dt1);
    output :
    1 SOP returns 11/07/2007 [current date]
    2 SOP returns Wed Nov 07 00:00:00 GMT+05:30 2007

    If you are using PreparedStatements setDate method, you need a java.sql.Date object, not a Java.util.Date object. You can create an sql.Date object from a util.Date object as follows:
    java.sql.Date sqlDate = new java.sql.Date(dt.getTime());And, if you are not using PreparedStatement and its setDate method, then change now.
    Doing
    Date dt1 = sdf.parse(sdf.format(dt));is meaningless. You format the date to string, then parse the string back to a date? Why? Why not just use the original Date object? And you won't get a sql.Date object that way, either, as the DateFormat classes return a util.Date object.
    Edit: And for more info:
    A Date does not have a format. It is simply a number. The "format" only comes into play when printing the String representation of the Date, and that is not handled by the Date object itself (except when calling toString either explicitly or implicitly).

  • How to convert string to date format?

    Hi All,
    String is in following format: 2006-12-07 i.e yyyy-mm-dd
    I want it in 07.12.2006 date format.
    which function module should i used to convert it?
    Regards,
    Nilima

    an other way to do this :
    Code
    DATA :
    ld_text(20) TYPE c,
    ld_date TYPE datum.
    ld_text = '2006-12-07'.
    REPLACE ALL OCCURENCES OF '-' IN ld_text WITH ''.
    WRITE ld_text TO ld_date.
    WRITE ld_date.

  • How to convert string into date datatype

    hi! there
    i've a date in string format like MM/dd/yyyy
    eg:String sDate = "01/30/2002";
    and i want to convert this string format into java date format
    same as 01/30/2002 only
    when i write like this
    SimpleDateFormat formatter = new SimpleDateFormat ("MM/dd/yyyy");
              String sDate = "1/11/2002";
              java.util.Date oDate = formatter.parse(sDate);
    i'm getting the output
    Fri Jan 11 00:00:00 GMT+05:30 2002
    i just want the out put like 01/30/2002
    plz,help me

    Hi,
    Just use back the SimpleDateFormat object you defined.
    String myDateInStr = formatter.format(oDate);
    this will format a java.util.Date object to a string representation according to the format you specified in the instantiation of SimpleDateFormat object.

  • How to convert string into date

    Hi all,
    I m new to JDeveloper
    I m taking the date as a string then passing it as a string in the method.
    I want to convert tht string into the date of format (DD-MON-YYYY) or any other format
    require yur assistance in this job.
    THANKXX in advance
    ankit

    Is this what you are looking for:
    java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("dd-MMMMM-YYYY");          
    java.util.Date date1 = null;
    try
       date1 = formatter.parse("Thu Jun 3 09:09:30 2004");
    catch(Exception e)
       e.printStackTrace();
    System.out.println("Date1 in millis : " + date1.getTime());
    System.out.println("Date1 in string : " + date1);
    java.util.Date date2 = new java.util.Date(date1.getTime());
    System.out.println("Date2 in string : " + date2);or look at http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html for more options

Maybe you are looking for

  • Are "Added to Folder" events queued?

    I have a Folder into which someone is transfering (FTP) multiple files to me, and   I have an AppleScript, which is processing the files. (As a side note, the Script is properly making sure all the files are properly transferred before the Script pro

  • Intermittent Workspace/Shared Services issue - 11.1.2.1

    All Version 11.1.2.1 Using Shared Services with Native (1) and MSAD (2) ; IDs are members of Native groups which are provisioned to the application We are experiencing an intermittent problem in Workspace where one day we can login without issue, and

  • Lost folders on iPhone - apps everywhere - can i restore to the mode before this happened ?

    My apps sync went wrong and now my 180 apps are not in folders that I previously had. My question is can I restore to a mode from a few days ago before it happened and the apps were in folders.. does it help that my ipad apps stay and are still in fo

  • Downloaded files aren't on my computer (searched everywhere)

    Hi, I am having trouble with some PDF files I downloaded. Completely downloaded about 12 files I needed, but from the download viewer I only moved the 4 I needed to work with into a folder. I figured the rest were stored in some temporary folder and

  • GR55-Report Writer output

    Hello Experts, I have a requirement to get the output from Report writer (GR55) and send it as an XML attchment in a mail to receipient. I have used the SUBMIT statement to access the GR55 in my Custom coding. I could find an option in GR55 to export