Results 1 to 5 of 5
- 07-31-2011, 01:54 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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-
- 07-31-2011, 02:24 PM #2
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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;
}
- 07-31-2011, 03:41 PM #3
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
- 07-31-2011, 08:15 PM #4
Member
- Join Date
- Mar 2011
- Posts
- 21
- Rep Power
- 0
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-
- 08-01-2011, 10:29 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Inserting current date to oracle Date data type
By bartolay13 in forum JDBCReplies: 5Last Post: 02-16-2012, 09:26 AM -
Incompatible type for method. Can't convert java.lang.String to char.
By renu in forum New To JavaReplies: 1Last Post: 07-27-2010, 06:01 PM -
Java, MySQL and Data Type Float
By digidigdj in forum AWT / SwingReplies: 0Last Post: 03-11-2010, 07:42 PM -
Field type
By code_worm in forum New To JavaReplies: 15Last Post: 10-21-2009, 03:03 PM -
[SOLVED] How to check what type of value entered in text field
By Renegade85 in forum New To JavaReplies: 2Last Post: 04-28-2008, 10:26 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks