Convert date to integer and back

S

Steven

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.
 
N

Niels Dybdahl

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
 
A

Arnaud Berger

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
 
T

Tor Iver Wilhelmsen

Steven said:
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.
 
B

Boudewijn Dijkstra

Tor Iver Wilhelmsen said:
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.
 
B

Boudewijn Dijkstra

Boudewijn Dijkstra said:
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.
 
B

Betty

Boudewijn Dijkstra said:
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;
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top