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);

Similar Messages

  • Need a little help converting String to date

    been struggling with different variations of this after searching these forums for a while but here it is:
    I have saved in a database, the date in String format created from this code:
    Calendar cal = Calendar.getInstance();
    java.util.Date date = cal.getTime();
    long time = date.getTime();
    String today = String.valueOf(time);
    The variable "today" is what I save in the DB table
    Now, I want to retrieve the String and it comes in like this for example:
    '1061438833530'
    Now I need to convert this into:
    'August 21, 2003 12:08 AM'
    I have been struggling with the SimpleDateFormat object but I either get a "Unparseable date" error or it just doesn't like the data type I'm using.
    any help is greatly appreciated...thanks in advance,
    Chuck

    Date d = new Date(1061438833530L);
    System.out.println(d);
    But you probably aren't storing the date in the DB as a long. It's probably a timestamp or some kind of date. Use
    rs.getDate()
    or
    rs.getTimestamp()

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

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

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

  • SimpleDateFormat: Parse String To Date

    Hi
    I'm trying to parse Date-String to Date. But the Date-String I want to parse is embedded in a Text like:
    Bla bla bla bla bla bla 2004-05-05 bla bla bla bla bla.
    So I tried the attached Class to solve the problem, but it didn't work.
    Any ideas?
    Thanks Bona
    public class Test {
    private static SimpleDateFormat dateFormat = new SimpleDateFormat();
    private String text;
    private Date date;
    public Test() {
    /ry {
    text = "Bla bla bla bla bla bla 2004-05-05 bla bla bla bla bla.";
    date = toDate(text, "yyyy-MM-dd");
    System.out.println("Datum: "+date);
    } catch (ParseException pe) {
    System.out.println(pe.getMessage());
    public static void main(String[] args) {
    Test test1 = new Test();
    public static Date toDate(String dateString, String dateFormatPattern)
    throws ParseException {
    Date date = null;
    dateFormat.applyPattern(dateFormatPattern);
    dateFormat.setLenient(true);
    date = dateFormat.parse(dateString);
    return date;
    }

    Well, you need to extract the date pattern from that String.
    You can use regular expressions to do that. See theRegex package for info on how to do that.
    BTW - AFAIK, lenient date parsing means, that the parser accepts stuff that matches the pattern, but contains illegal values like "2004-02-30" ...

  • 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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • 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.
    %

  • Converting string into date

    How can I convert a string 07-27-2001 in a date.

    How can I convert a string 07-27-2001 in a date.
    one of the solutions might be --
    SimpleDateFormat format = new SimpleDateFormat ("MM-dd-yyyy");
    Date date = format.parse("07-27-2001");

  • Converting String to Date (java.sql.date)

    Hi,
    Kindly let me know how I can convert a string like
    "20061102" (YYYYMMDD) to a Date type (java.sql.date) in web Dynpro.
    Requirement is to set a DateType context Attribute with
    the value contained in the String ("20061102" ).
    Awaiting your kind responses..
    Regards,
    Mahesh

    SimpleDateFormat yyyyMMdd_parser = new SimpleDateFormat(
                   "yyyyMMdd");
    java.util.Date date=yyyyMMdd_parser.parse("20061102");
    java.sql.Date slqDate=new java.sql.Date(date.getTime());

  • Issue with converting string to date object

    Hi all,
    I have a problem with sorting a list of dates in a table column. My dates are in the format 'yyyy-Mmm-dd'(Eg: 2009-Dec-23). Can someone help me how to sort the dates in this format. here is the code which i tried.
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-Mmm-dd");
              Date d1= new Date();
              Date d2= new Date();
              try {
                   d1 = dateFormat.parse("2006-Jun-09");
                 d2 = dateFormat.parse("2006-Apr-10");
              } catch (ParseException e) {
                   // TODO Auto-generated catch block
                   e.printStackTrace();
              System.out.println("converted date 1"+ d1);
             System.out.println("converted date 1"+ d2);
    Exception : Unparseable date: "2006-Jun-09"
         }Please someone help me how to pass this date in the right format.

    I believe twisai may have lost the documentation so he/she can figure this out rather than guess that "Mmm" means 3-letter month, first letter capitalized. Let me remind him of the link:
    [http://java.sun.com/javase/6/docs/api/index.html]

  • 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);

  • 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).

  • 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 :)

Maybe you are looking for

  • Alv grid data

    Hi All,    I developed alv grid.I passed data from it_final table into grid.In our it_final table, there is 10 rows and when I execute report then it comes 10 line item.It is perfect. Now I do subtotal in any field.Then In the grid total 15 line item

  • Add Same Photo to a Group of Contacts

    Can anyone please help with an Applescript to add an image to a group of contacts in one foul swoop. My intention is to add a logo image to a group of colleagues. Here is what I have so far, - but receive this error tell application "Address Book" se

  • Ipad2 contact details are not displayed

    Hi guys, since a few days I have got the following problem on my iPad 2, OS 5.01, sync with iCloud. The contacts are displayed in 2 pages layout, left the alphabetical list of contacts and (usually) on the right the details of the selected contact. T

  • Moved all my files to my portable hard drive and put them back on my computer but all downloads - music and apps - aren't showing up on my computer

    I had a problem where I had to remove all of my picture, movie, and music files from my computer and now that I've put what I moved back onto my computer, I went to sync my iPod and realized that all of my previously downloaded apps and music files a

  • Server name

    I installed Personal oracle 8i on windows 98. When I connect with the database in control panel data source , microsoft ODBC for Oracle , it asked for server name. What should I use ? Thanks a lot.