Results 1 to 5 of 5
  1. #1
    fortwnty420 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Trying to convert Java Date to type int to fit in mysql table with field type int(11)

    I am trying to convert a date such as "dd-MM-yyy" "01/01/2011" to a type int(11) for a mysql database.

    The only thing I can find is a conversion to long using the Date class and the simple date formatter.

    .getDate is deprecated and only .getTime is available. (which returns type long). If I just cast it from a long to an int I totally lose the date.

    Any help would be greatly appreciated.

    Jeff-

  2. #2
    fortwnty420 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    This worked for me: The trick was to divide the long value by 1000 and this makes it small enough for an int.

    private java.util.Date end_date(String str_end_date ){

    DateFormat formatter;

    java.util.Date end_date = new java.util.Date();

    try{

    formatter = new SimpleDateFormat("MM-dd-yyyy");

    end_date = (java.util.Date)formatter.parse(str_end_date);

    VMProductDiscountsDataBag dtl = new VMProductDiscountsDataBag();
    dtl = this.arrDataBag.get(this.DetailIndex);
    dtl.end_date = Long.valueOf(end_date.getTime()/1000).intValue();
    dtl.IsDirty = true;
    arrDataBag.set(this.DetailIndex, dtl);

    }
    catch (ParseException e){
    System.out.println("Exception :"+e);
    }

    return end_date;

    }

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    If you're only concerned with the date and not the time-of-day, you can divide by (1000 * 60 * 60 * 24), or format and store it in ANSI date format (YYYYMMdd). But why on earth do you have an int(11) column for a date value in the first place? MySQL provides 3 data types (granted that one of them has the y2k38 bug).

    db

  4. #4
    fortwnty420 is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default I said the same thing.. why int(11) for date. :@:

    I didn't choose the design actually.. It is for virtue_mart. that is the table structure that the system uses.. I was only concerned with the actual date..

    Thanks for you help it is much appreciated!

    Jeff-

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    What does that int(11) represent?
    Can't really do any conversion without knowing what it is supposed to be...

Similar Threads

  1. Replies: 5
    Last Post: 02-16-2012, 09:26 AM
  2. Replies: 1
    Last Post: 07-27-2010, 06:01 PM
  3. Java, MySQL and Data Type Float
    By digidigdj in forum AWT / Swing
    Replies: 0
    Last Post: 03-11-2010, 07:42 PM
  4. Field type
    By code_worm in forum New To Java
    Replies: 15
    Last Post: 10-21-2009, 03:03 PM
  5. Replies: 2
    Last Post: 04-28-2008, 10:26 AM

Posting Permissions

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