Converting from milliseconds to a date format in java

This so that, that date can be inserted into a date column in mysql
What I have is something like 1119193190
I do:
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
Date resultdate = new Date(yourmilliseconds);
System.out.println(sdf.format(resultdate));
and Java gives me something like:
Jul 04,2004 14:06
But Java then when inserting into a mysql table is all like um....no:
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: 'Jul 04,2004 14:06' for column 'prodDate' at row 1
proDate is of type date in mysql.
Help?

jverd wrote:
"Jul 04,2004 14:06" is a String, not a Date.
PreparedStatement ps = conn.prepareStatement("insert into T(name, birthdate) values(?, ?)");
ps.setString(1, "Joe Smith");
java.sql.Date date = new java.sql.Date(yourmillis);
ps.setDate(2, date);
ps.executeUpdate();
I am a bit confused
This i what I have
for(int i = 0; i < productions.size(); i++)
                    //Create a new Production from the ArrayList
                    Production p = (Production) productions.get(i);
                    //Convert the date from milliseconds to YYYY-MM-DD format. for mysql?
                    SimpleDateFormat dateFormatter = new SimpleDateFormat("MMM dd,yyyy HH:mm");
                    Date convertedDate = new Date(p.getDate());
                    //Build a query to insert the Production into the table
                    String insertQuery = "INSERT INTO WELL_PROD VALUES(" +
                              "'" + p.getLocation() + "'," +
                              "'" + dateFormatter.format(convertedDate) + "'," +
                              "'" + p.getOilProd() + "'," +
                              "'" + p.getWaterProd() + "'," +
                              "'" + p.getGasProd() + "')";
                    //Print the query to the screen
                    System.out.println("-> INSERTING the following query into well_prod: ");
                    System.out.println("   " + insertQuery);
                    //Update the database using the constructed query
                    int result = statement.executeUpdate(insertQuery);
                    //Print out the result of the insertion
                    System.out.println("   INSERT RESULT: " + result);
               }Are you saying something like
java.sql.Date date = new java.sql.Date(Something , what I have no idea, Should go here);
instead of
SimpleDateFormat dateFormatter = new SimpleDateFormat("MMM dd,yyyy HH:mm");
and then carry on as normal?
If so, what should go in those brackets based on the code?
java.sql.Date date = new java.sql.Date("MMM dd,yyyy HH:mm");
This is all being read in from a text file and converted over before being spit out to the data base, it all works except for the date...

Similar Messages

  • IPod Classic 160GB film library sync problem -  trying to sync films that have converted from purchased DVD to mp4 format locally, not purchases from the online store - all other libraries sync perfect - why?

    Hello Apple and the iTunes Windows PC users community.
    I am trying to sync films that I have converted from purchased DVD to mp4 format locally, not purchases from the online store. The mp4s all appear and play successfully in my iTunes application but will not sync across to the iPod film library folder.
    For your information: I am using iTunes 11.1.3.8 on a Windows 7 64bit machine with 500GB hard disk and 8GB of Ram in the UK.
    I have restored the iPod classic 160GB three times now to see if it was a hardware problem with no joy. Each time all the music restores properly as do the podcasts and all the items in TV programmes all appear to sync and work fine.
    I have also tried to copy films into the TV Programmes to get around it with no joy. They always go to the films section to start with. It is just the Films library that does not sync - all others work perfectly. As a last resort I have uninstalled and re-installed iTunes with no joy either.
    I am technically savvy and have gone through the itunes and ipod settings but nothing appears to make a difference - This is the first time I have had to post here as I can usually solve the majority of the ipod anomilies but this one has me flummoxed.
    Has the film encoding type changed in the newest itunes update? - Has this happened to anybody else and is it a hardware, software, or operating system problem.

    Having uninstalled the current version 11.1.3.8 and loaded and older version of iTunes 10.7.0.21 I can now categorically confirm that the newest update seems to be causing the problem as the films and TV Programmes are syncing perfectly on this older version.
    If you are going to do this please dont forget to remove the  ' iTunes Library.itl' file as this stops the older versions from running as I've just found out

  • Does anyone know a good converter from quicktime movies to dvd format?

    Does anyone know a good converter from quicktime movies to dvd format?

    I'm guessing that your using One Step DVD . This will only work with one movie at a time.
    Create a new project and all you should have to do is drag your movies from your movie folder onto your iDVD project. You can add as many movies as you like but obviously you can't exceed the maximum total DVD size.
    Encoding can take a long time depending on the original file format, but you should be able to use pretty much any file format you can play with Quicktime.
    Message was edited by: Noiseboy2006

  • Converting httpSession creation time to Date format

    I have the need to convert the value returned from httpSession.getCreationTime() to a Date format. The value returned from getCreationTime is long. I need to parse this value into 2 other fields. The first needs to be in year, month, day format. The second must give me the time in hours, minutes, seconds and milliseconds. Thanks in advance for any help you can give me.

    Simple:Date date = new Date(httpSession.getCreationTime())

  • Convert time in miliiseconds to date format

    I have a column in my table with datatype number(38,0) , in that column i have values in form of miliseconds since 1970 e.g 1152567973042...
    i have a requirement to convert this time in milliseconds to today's date, which i can do as
    1152567973042 / 3600 / 24 / 365 == x
    1970 + x = today's date
    is there a function in oracle supporting this, or i have to customize my requirements and write a function.
    Thanks in anticipation

    Your function will not work precisely because it does not account for leap years. You should use Oracle date math. If you add a number to an Oracle date datatype, it assumes that the number is in days. Oracle will automatically account for leap years. Try this:
    DECLARE
    mydate DATE;
    milliseconds_since_1970 NUMBER(30,7);
    BEGIN
    milliseconds_since_1970 := ?;
    mydate := TO_DATE( '01-JAN-1970', 'DD-MON-YYYY') + milliseconds_since_1970/(1000*60*60*24);
    DBMS_OUTPUT.PUT_LINE('mydate = ' || to_char( mydate, 'DD-MON-YYYY HH24:MI:SS' ) );
    END;
    This converts your milliseconds (replace ? with the number) to days and then adds the days to January 1, 1970. The result is an Oracle date datatype adjusted for leap years.
    Oracle date datatypes have hours, minutes, and seconds. Since these were not specified for 1/1/1970, they default to zero. Since milliseconds_since_1970 has 7 decimal places, it will contain fractional days which Oracle converts to hours, minutes, and seconds.
    I'm not sure what format Oracle stores date datatypes in. You have to convert them with a format string every time you use them.
    Kevin Tyson

  • How to convert parameter in to proper date format in unix.

    My requirement is to convert parameter string (012011) as a date in unix without support from other like sql. then once it is converted into date format how to increment it by one day. Here i want to use my own date rather than system timestamp of unix system. Please help.
    I am unix HP unix 11.

    Moderator Action:
    Your question is outside the scope of this Linux forum space, and is locked.
    You seem to hope for assistance with a specific Unix that is a product from a specific company.
    I suggest you go to that company's forum community and ask there.  You will undoubtedly find many people there that are familiar with the nuances if that OS.

  • Converting the string value to data format

    Hello Everyone,
                                  Please guide me in converting the value to date format.
    from source i'm getting the value (which is acchally a data value) '20070730'.
    I need this value to be in date format '2007/07/30'
    Please help me in getting it done.
    thank you

    Hi
    Code Snippetselect cast('20070730' as datetime)
    Note : beware of collation used in your SQL instance or your database.
    Jean-Pierre

  • Adding milliseconds to long date format (tzntstmpl)

    I have a date field called START_DATE defined as type TZNTSTMPL (long date/time format - YYYYMMDDHHMMSS.mmmuuun) and another field call MILLISECONDS also defined as type TZNTSTMPL.
    I want to subtract 625 milliseconds from START_DATE, so I put .625 in the MILLISECONDS field and subtract MILLISECONDS from START_DATE.
    The starting value in START_DATE is "20090701095000.5410000". After I subtract MILLISECONDS from START_DATE the result is "20090701094999.9160000". 
    The result should have been "20090701094959.9160000. The problem is with SS (Seconds) 99 is invalid.
    How can I get it to subtract correctly?
    Are there any ADD or SUBTRACT functions that work with the long date/time format?
    Regards,
    Mike...

    Vindo,
    Thanks that is exactly what I was looking for. It handled the adding and subtract from the long date format correctly.
    Regards,
    Mike...

  • How to convert a string into orcle date format

    i need some information about converting string into date format.i have string like '20-SEP-05' .so we have to convert into date formate like yy-mm-dd' (2005-09-20).how can we do it.

    Here's some code to help. I can't remember what method uses what format though
      public static java.sql.Date sqlDateValueOf( String dateString ) {
        String stringDate = null;
        if ( dateString == null || dateString.length() < 10 ) return null;
        String strDay = "";
        String strMonth = "";
        String strYear = "";
        if ( dateString.substring(4,5).equals( "-" ) ) {
          strDay = dateString.substring(8, 10);
          if ( strDay.length() < 2 ) strDay = "0" + strDay;
          strMonth = dateString.substring(5, 7);
          strMonth = dateString.valueOf(Integer.parseInt(strMonth) - 1);
          if ( strMonth.length() < 2 ) strMonth = "0" + strMonth;
          strYear = dateString.substring(0, 4);
          stringDate = strDay + "-" + strMonth + "-" + strYear;
        else if ( dateString.substring(2,3).equals( "-" ) ) {
          strDay = dateString.substring(0, 2);
          if ( strDay.length() < 2 ) strDay = "0" + strDay;
          strMonth = dateString.substring(3, 5);
          strMonth = String.valueOf(Integer.parseInt(strMonth) - 1);
          if ( strMonth.length() < 2 ) strMonth = "0" + strMonth;
          strYear = dateString.substring(6, 10);
          stringDate = strDay + "-" + strMonth + "-" + strYear;
        Calendar cal = Calendar.getInstance();
        cal.set( Calendar.YEAR, Integer.parseInt( strYear ) );
        cal.set( Calendar.MONTH, Integer.parseInt( strMonth ) );
        cal.set( Calendar.DAY_OF_MONTH, Integer.parseInt( strDay ) );
        java.sql.Date outDate = new java.sql.Date( cal.getTimeInMillis() );
        return outDate;
      public static String toDateString( java.util.Date date) {
        if ( date == null || date.toString().length() < 10 ) return null;
        String outDate = "";
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        outDate = df.format( date );
        return outDate;
      public static String toDateString( java.sql.Date date) {
        if ( date == null || date.toString().length() < 10 ) return null;
        String outDate = "";
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        outDate = df.format( date );
        return outDate;
      public static String toDateString( java.sql.Date date, String format) {
        if ( date == null || date.toString().length() < 10 ) return null;
        String outDate = "";
        SimpleDateFormat df = new SimpleDateFormat( format );
        outDate = df.format( date );
        return outDate;
      public static String toDateString( java.sql.Timestamp date) {
        if ( date == null || date.toString().length() < 10 ) return null;
        String outDate = "";
        SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        outDate = df.format( date );
        return outDate;
      }

  • Converting Oracle 7-Byte Internal Date FOrmat to MM/DD/YYYY format.

    I can get the date stored in the database in 7-Byte Internal representation.
    I want to convert this number to mm/dd/yyyy format,using PRO*C-SQLDA(Method 4),overriding any NLS DATE Format.
    null

    Only Oracle Spatial questions
    here please.
    null

  • Very Very Urgent!!!!Converting from Hijri To Gregorian Date

    Hi ABAP Gurus,
    Is there any  function module to convert the hijri date to Gregorian Date and vice versa.
    i have seen the code in one of the thread but it is not converting the hijri to gregorian dates accurately.
    Please help me to resolve the issue.....
    Any Suggestion will be highly appreciated and rewarded.
    Regards
    Mustafa

    Look at OSS [Note 831333 - Enable Japanese emperor, Islamic, and Farsi date format|https://service.sap.com/sap/support/notes/831333] and [Note 882610 - Hijri Calendar support|https://service.sap.com/sap/support/notes/882610]
    Regards

  • Entity DATE type attribute : Derieved From SQL Expression for date format

    Hi,
    I want to set one of the Entity's Date Attribute with specific format , for e.g DD-MM-YYYY
    I see a Derieved From SQL Expression checkbox, how can I define the SQL Expression
    can I use TO_DATE(EMP_START_DATE,'DD-MM-YYYY'), I want to insert a date in that format, when I am creating a row using viewObject.createRow()
    Java Type is oracle.jbo.domain.Date which take YYYY-MM-DD as a string, I do not want to use this format
    Thanks,

    Here is a solution, but I am sure it is not the best one. It will work in a hurry. Maybe you can create a helper method to generalize this conversion until something more succinct comes along for US:
    This code assumes an import of jbo.oracle.domain.Date.
        public void updateDateTest () {
            AddressesViewImpl lVO = (AddressesViewImpl)this.getAddressesView1();
            AddressesViewRowImpl lRow = (AddressesViewRowImpl)lVO.first();
            System.out.println("create date for current record is currently: " +
                               lRow.getCreationDate());
            java.util.Date today = new java.util.Date();
            SimpleDateFormat dateFormat =
                new SimpleDateFormat("dd-MM-yyyy");
            SimpleDateFormat jboDateFormat =
                new SimpleDateFormat("yyyy-MM-dd");
            String lSampleDateString = "15-04-2010";
            java.util.Date lSampleDate = null;
            try {
                lSampleDate = dateFormat.parse(lSampleDateString);
            } catch (ParseException e) {
                System.out.println("Parsing exception thrown:  " + e.getMessage() +
                                   "\n ==> caused by \n==>"+ e.getCause().getMessage());
                lRow.setCreationDate(new Date(jboDateFormat.format(lSampleDate)));
            System.out.println("about to commit; create date for current record is currently: " +
                               lRow.getCreationDate());
            this.getDBTransaction().commit();
            System.out.println("resetting to some other date; create date for current record is currently: " +
                               lRow.getCreationDate());
                lRow.setCreationDate(new Date(jboDateFormat.format(today)));
            this.getDBTransaction().commit();
        }I defined this code in my Application Module Impl file and ran it with the BC tester. Here was its output:
    Mar 18, 2010 8:27:54 AM oracle.jbo.jbotester.MainFrame main
    INFO: BC4J Tester started.
    Source breakpoint occurred at line 66 of FusionExperimentsAMImpl.java.
    create date for current record is currently: 2009-02-02 12:09:54.0
    about to commit; create date for current record is currently: 2010-04-15
    resetting to some other date; create date for current record is currently: 2010-04-15I spent a little time looking around the forum for additional solutions. I think several years ago I even wrote a blog entry on this subject. If I remember how to do this right I will amend with more information.

  • Date Format of Java Calendar

    Can someone give me the date format that is returned by the calendar?
    Thanks.

    How about if you want to specify/display the time, what is the default?
    I tried using Date Picker (MM/DD/YYYY HH24:MI). When I click on the calendar icon, the default date is today's date, but the default time is set to +5 hours ahead. Any way to make the default time, the system's default time? I also checked out database sysdate - and the time is correct in there.
    It works perfectly if I set the default value to be to_char(sysdate, 'MM/DD/YYYY HH24:MI') it displays the correct date and time. But the users of the app do not want the default date/time to be displayed in the field, they want to manually pick it from their calendar themselves with the default on the calendar popup to displays today's date and the current time.

  • Converting a string into mySQL date format

    I have a bit of code that collects data values from a bean and I want it to then insert this into a mySQL database. My string is in the format "23/12/1983", how can i put this into a date field in my database? The bit of code I have got so far looks like this...
    String month = formHandler.month + "/";
    String day = formHandler.day + "/";
    String year = formHandler.year ;
    StringBuffer sb = new StringBuffer();
    sb.append(day);
    sb.append(month);
    sb.append(year);
    String myDate = sb.toString();myDate is the value that I want to go into the database

    Limo2kz,
    The key to inserting a date into a database is to convert the date string into an SQL friendly format. i.e. yyyy-MM-dd.
    One way of doing this is as follows:
    SimpleDateFormat NICE_DATE_FORMAT =
    new SimpleDateFormat("dd/MM/yyyy");
    SimpleDateFormat SQL_DATE_FORMAT =
    new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date();
    try {
    date = NICE_DATE_FORMAT.parse(dateString);
    } catch (ParseException pe) {
    pe.printStackTrace();
    String sqlDate = SQL_DATE_FORMAT.format(date);
    Now it is just a matter of creating the insert statement like normal, and making sure that the date string is in quotes.
    I hope that helps.
    Damian Sutton

  • Convert string (in a Julian Date format) to a date in CR XI Release 2

    How do you convert a string field  entered in a database as a Julian date " 10109 , 09008. ,,," to
    print on a report as  date?
    09008 would be 01/08/09
    10109                 04/19/10

    A small correction to Brian's formula:
    NumberVar myYear;
    NumberVar myDays;
    myYear := ToNumber( {table.JULIAN} [1 to 2]);
    myDays := TONUMBER({table.JULIAN} [3 to 5]);
    (Date (myYear, 1, 1) + myDays) - 1;
    The result for '09008' will be 01/08/9, by adding ToText, you can get the result in the desired format of 01/08/09.
    totext((Date (myYear, 1, 1) + myDays) - 1, "MM/dd/yy");
    Here is an alternate solution without having to create variables:
    totext(dateserial(tonumber(X[1 to 2]),1,tonumber(X[3 to 5])),"MM/dd/yy");
    where X is the julian date in string.
    Edited by: Sanjay Kodidine on Apr 20, 2010 8:25 AM

Maybe you are looking for