0

I have a database that holds times at which i need to run certain processes I do a query which gives me the difference between the current time and the time at which i'm supposed to run the process For example, I might get this number 23:49:44.835 which means i need to run the process in about 23 hours. I want to get this process in milliseconds, so I have done this

Date Date = (java.util.Date) (InMsgTstable[index][0]);
long Value3 = Date.getTime();

The date value i get is correct, though it drops the milliseconds value that is stored in my database. I want to get this number of hours/minutes etc.. in milliseconds so i can put that number as a millisecond delay in a scheduler. However, this is giving me the wrong value in milliseconds. Any tips?

3
  • To clarify, the numbers you get are off by upto +- 999 millis, right? You are not saying that your numbers are off by a factor of 1000... Jul 18, 2012 at 20:08
  • I'm a bit confused here. You get the correct hours, minutes, seconds and millis in your Date object?
    – Keppil
    Jul 18, 2012 at 20:15
  • well as it stands now, if i have the following hours i get the following milliseconds 22:38:56 99536699 if i convert i see that's about 5 hours off. not quite sure what the cause of this is. also i'm not sure if getTime gets time since 1970 because i don't want that
    – LemonMan
    Jul 18, 2012 at 20:20

1 Answer 1

1

If you are in control of the database table, I would change the column type to an integer type and simply store the timestamp in milliseconds as an integer value.

That will allow you to treat the value as the timestamp that Date and Calendar use natively without having to handle the conversions that databases perform on date and time columns.

Edit

From your comment above I see that the value you get looks like the number of milliseconds since the start of the current day (which is by definition localtime, so beware of timezone values in the database.)

The date that you create using this timestamp is situated in Jan 1 1970, which is not what you want.

What you could do is to create a calendar object which defaults to "now", back-up to the start of day and get that timestamp to add to the value you got from the database, something like (untested):

Calendar cal = Calendar.getInstance();

cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

long stamp = cal.getTime().getTime() + msgTableValue;

Date dat = new Date(stamp);

which should give you a Date object representing msgTableValue milliseconds into the current day.

11
  • the database is actually a kdb+ database and the dates are q objects which i can convert to a java date
    – LemonMan
    Jul 18, 2012 at 20:19
  • @MatthewPiatetsky, what is actually stored in the q object? An integer long value or a date/time value? Storing the timestamp in the form of a long integer saves you on interpretation to/from the database store.
    – rsp
    Jul 18, 2012 at 20:23
  • not quite sure but i'm not in control of this database I'm just writing a program for it. the q objects are used for real time high volume ticker data and stuff but when i bring in the times with a method i can get it as a date object. i think q has its own class for timestamps or something
    – LemonMan
    Jul 18, 2012 at 20:26
  • this looks pretty close. just a bit confused though what exactly is msgTableValue? Is it the date object i was getting? Because i was getting a date object from the database and then using getTime
    – LemonMan
    Jul 18, 2012 at 20:55
  • @MatthewPiatetsky, yes that was the idea - msgTablevalue would be your millies value, it looks like what you get from the database only has the millies part of a date, so you need to add it to the start of the current day in millies to get a valid timestamp.
    – rsp
    Jul 18, 2012 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.