Convert date to integer and back

Discussion in 'Java' started by Steven, Apr 1, 2005.

  1. Steven

    Steven Guest

    Hi everyone,

    How would I convert a date to an integer and back again?

    For example :

    March 29, 2005 -> Integer

    Integer -> March 29, 2005

    Thanks for any help!

    Regards,
    Steven.


    --
    Posted by news://news.nb.nu
     
    Steven, Apr 1, 2005
    #1
    1. Advertising

  2. > How would I convert a date to an integer and back again?
    >
    > For example :
    >
    > March 29, 2005 -> Integer
    >
    > Integer -> March 29, 2005


    How about Date.getTime() and Date(long date) ?

    Niels Dybdahl
     
    Niels Dybdahl, Apr 1, 2005
    #2
    1. Advertising

  3. Hi,


    What about converting to/from a "long" (can represent timestamp) ?

    long someLong=1115330300000L;

    Date date=new Date();
    // from Date to long
    long currentTime=date.getTime();
    // from long to Date
    Date anotherDate=new Date(someLong);

    Regards,

    Arnaud

    "Steven" <> a écrit dans le message news:
    424cf573$...
    > Hi everyone,
    >
    > How would I convert a date to an integer and back again?
    >
    > For example :
    >
    > March 29, 2005 -> Integer
    >
    > Integer -> March 29, 2005
    >
    > Thanks for any help!
    >
    > Regards,
    > Steven.
    >
    >
    > --
    > Posted by news://news.nb.nu
     
    Arnaud Berger, Apr 1, 2005
    #3
  4. "Steven" <> writes:

    > How would I convert a date to an integer and back again?


    You cannot, but you can use getTime() on a Date object to get a long,
    which you then can pass to the Date constructor.
     
    Tor Iver Wilhelmsen, Apr 1, 2005
    #4
  5. "Tor Iver Wilhelmsen" <> schreef in bericht
    news:...
    > "Steven" <> writes:
    >
    >> How would I convert a date to an integer and back again?

    >
    > You cannot, but you can use getTime() on a Date object to get a long,
    > which you then can pass to the Date constructor.


    If you divide the long by 1000, then you can fit the value into an int. Well,
    at least until 18 jan 2038.
     
    Boudewijn Dijkstra, Apr 1, 2005
    #5
  6. "Boudewijn Dijkstra" <> schreef in bericht
    news:424dccc6$0$44082$...
    > "Tor Iver Wilhelmsen" <> schreef in bericht
    > news:...
    >> "Steven" <> writes:
    >>
    >>> How would I convert a date to an integer and back again?

    >>
    >> You cannot, but you can use getTime() on a Date object to get a long,
    >> which you then can pass to the Date constructor.

    >
    > If you divide the long by 1000, then you can fit the value into an int.
    > Well, at least until 18 jan 2038.


    And if you want a date without time, you could use a short with no. of days
    since 1 jan 1970, which expires 18 aug 2059.
     
    Boudewijn Dijkstra, Apr 24, 2005
    #6
  7. Steven

    Betty Guest

    "Boudewijn Dijkstra" <> wrote in message
    news:d4fums$770$...
    > "Boudewijn Dijkstra" <> schreef in bericht
    > news:424dccc6$0$44082$...
    > > "Tor Iver Wilhelmsen" <> schreef in

    bericht
    > > news:...
    > >> "Steven" <> writes:
    > >>
    > >>> How would I convert a date to an integer and back again?
    > >>
    > >> You cannot, but you can use getTime() on a Date object to get a long,
    > >> which you then can pass to the Date constructor.

    > >
    > > If you divide the long by 1000, then you can fit the value into an int.
    > > Well, at least until 18 jan 2038.

    >
    > And if you want a date without time, you could use a short with no. of

    days
    > since 1 jan 1970, which expires 18 aug 2059.
    >


    I like to convert to julian and back, this way the int can be used
    for something like finding the number of days between two dates.

    class Jday {

    // y is 4 digit, 2004, for example
    int jday(int y, int m, int d) {
    int M1 = (m - 14) / 12;
    int Y1 = y + 4800;
    int J = 1461 * (Y1 + M1) / 4 + 367 * (m - 2 - 12 * M1) / 12
    - (3 * ((Y1 + M1 + 100) / 100)) / 4 + d - 32075;
    return J;
    }
    }
     
    Betty, Apr 24, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,278
    Michael Borgwardt
    May 30, 2004
  2. les ander
    Replies:
    4
    Views:
    887
    wes weston
    Oct 5, 2004
  3. Replies:
    5
    Views:
    1,935
    Wesley Hall
    Nov 4, 2006
  4. Harlin Seritt
    Replies:
    29
    Views:
    1,187
    Paul Rubin
    Feb 24, 2007
  5. jason

    Re-convert Currency back to Integer?

    jason, Sep 1, 2003, in forum: ASP General
    Replies:
    1
    Views:
    173
    jason
    Sep 1, 2003
Loading...

Share This Page