5 Replies Latest reply: Nov 23, 2007 9:06 AM by 807603 RSS

    convert double to Date

      hi everyone, i need to convert a double to a Date. for example 39080.0 is 29/12/2006. could anyone help me??? thanks
        • 1. Re: convert double to Date
          I think a good place to start would be to tell us your conversion rules.
          • 2. Re: convert double to Date
            is like in excell that if you put a number 39080 and you change the format to a date they put 29/12/2006. i need that function but in java.
            • 3. Re: convert double to Date
              Then you are going to have to ask MS how they do it, and implement it in Java, since MS came up with a "date/time" system for excel that differs from every other one. Or search for an API that might help with it.
              • 4. Re: convert double to Date
                So 39080 is what? It might be the number of days since 1-1-1900 from the looks of it.

                Knowing that Java stores dates as number of milliseconds since 1-1-1970, it shouldn't be too hard to make the conversion.
                • 5. Re: convert double to Date
                  As I recall Excell dates are number of days since 1-1-1900, except that they mistakenly took 1900 to be a leap year, which it wasn't, making them 1 too low.

                  Now java dates are based on the number of milliseconds since 1/1/1970. So all you should need to do is to add one, multiply them by the number of milliseconds in a day, and add them to the epoch date of 1/1/1900 (which will be a large negative number which you can calculate with Calendar).

                  Suggest you cast to long first.

                  ps. One caveat: When doing this kind of calculation DST (Daylight Saving time) can trip you up. Because 1/1/1900 was outside of DST periods if you do these sums and the resultant date is inside the DST periods you are likely to wind up with 23:00 of the previous day - for safety add a couple of hours, load the result into Calendar, then set the time portion of the Date to zero.

                  Edited by: malcolmmc on Nov 23, 2007 3:03 PM