How to convert a Date to int

T

Terry Jolly

I'm new to Java, have been using vb6.

How do I do this in java?

VB6:

Dim lDate as Long

lDate = CLng(Date)

Java:

int lDate;

then what?


Thanks In Advance.
 
?

=?ISO-8859-1?Q?Jan_Thom=E4?=

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Terry said:
I'm new to Java, have been using vb6.

How do I do this in java?

VB6:

Dim lDate as Long

lDate = CLng(Date)

Java:

int lDate;

then what?


Thanks In Advance.
You have to use a long as well. I guess the long holds the number of
milliseconds in the current era in vb, so the equivalent in java would be:

long l = System.currentTimeMillis();

Greetings,
Jan

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFEWin9F3VbzQNZCsARAs1TAJ0XntYHUApN3tSSj311F0QsQK1+mACgimVk
FTcWPu8W51tFw8j5s+9L3ok=
=bbgk
-----END PGP SIGNATURE-----
 
B

Bjorn Abelli

Jan Thomä said:
Terry Jolly wrote:
You have to use a long as well. I guess the long holds
the number of milliseconds in the current era in vb, so
the equivalent in java would be:

long l = System.currentTimeMillis();

Though correct from a Java perspective, it doesn't correspond to the VB6
Date, which internally is represented by a *double*, where the fraction is
the time. The "integral" part is hence the number of *days* since its epoch:
1 January 1900

The question should rather be:

From where are you getting that int,
and how are you going to use the date?

// Bjorn A



Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 
T

Terry Jolly

Jan,

Thanks for the info. However it date number needs to be for example:

05/04/2006 would equal 38841. Which, what is stored in the database. Just
like MS Excel would display the a date if formated as a number with zero
decimal places.
 
L

Lasse Reichstein Nielsen

(please don't top post)
05/04/2006 would equal 38841.

I assume that date means the 4th of May.

Based on this, it appears the number is based such that 1 corresponds
to the 1st of January 1900.

Anyway, a simple way to get the 38841'th day after 1st of January 1900
is:
var theDate = new Date(1900,0,38841);
Which, what is stored in the database. Just
like MS Excel would display the a date if formated as a number with zero
decimal places.

The results given by Excel and Javascript differ, since they disagree
on whether there was a 29th of February 1900. Excel says yes, Javascript
no. Javascript appears to be right by the Gregorian calendar :)
<URL:http://www.tondering.dk/claus/cal/node3.html#SECTION00321000000000000000>

To compensate, you might want to subtract one to numbers >=61 before
passing them to Javascript (assuming all dates are in the range
1900..2099, otherwise you'll have to figure out what other
discrepancies there might be)

/L
 
T

Terry Jolly

The database i'm writing too stores dates as int32 --05/04/2006 would equal
38841. Just like MS Excel would display the a date if formated as a number
with zero decimal places.
 
L

Lasse Reichstein Nielsen

Lasse Reichstein Nielsen said:
Anyway, a simple way to get the 38841'th day after 1st of January 1900
is:
var theDate = new Date(1900,0,38841);

Whoops, forgot what newsgroup I was in. The method still holds, the
format is just a little different:

java.util.Calendar cal = new java.util.GregorianCalendar();
cal.setLenient(true);
cal.set(1900,0,38841);
java.util.Date theDate = cal.getTime();

The result is still the 5th of May, not the 4th as in Excel.

/L
 
T

Terry Jolly

Thanks Lasse,

Yes, I meant today May, 4, 2006.

In your example you're using 33841 (var theDate = new Date(1900,0,38841);)
which is what I'm trying to get from the Date. Maybe I'm mis-understanding
you.

I need to convert today, tomorrow, a pass date, etc., into a number like
38841 (May 4, 2006). Also the database I'm working with is storing the dates
in an int field as a number for example May 4, 2006 is stored as 38841. The
Database has lots of tables and all kind of dates (that are being store as a
number in a int field). I need to be able now only to convert the number
into a date, but also takea date and store it as an int number.
 
T

Terry Jolly

This also poses a problem. The database has roughly 30 tables, with close to
1m records and almost each has at least one field using an int to store a
Date number.
The date number matches what Excel would produce exactly. Since this is an
accounting system I have to be consistent with dates currently being
utilized.
 
S

Steve Wampler

Terry said:
Thanks Lasse,

Yes, I meant today May, 4, 2006.

In your example you're using 33841 (var theDate = new Date(1900,0,38841);)
which is what I'm trying to get from the Date. Maybe I'm mis-understanding
you.

I need to convert today, tomorrow, a pass date, etc., into a number like
38841 (May 4, 2006). Also the database I'm working with is storing the dates
in an int field as a number for example May 4, 2006 is stored as 38841. The
Database has lots of tables and all kind of dates (that are being store as a
number in a int field). I need to be able now only to convert the number
into a date, but also takea date and store it as an int number.

Can't you just use simple arithmetic now that you know the date 'count'
starts on 01/01/1990? You can use the GregorianCalendar class to get
the time in milliseconds for both the date you're interested in and for
01/01/1990, subtract and then divide by the number of ms in a day.
You may need to add one if you think of 01/01/1990 as day 1 instead
of day 0 and *maybe* add a leap-year adjustment if Excel really doesn't
get that right.
 
R

Roedy Green

Thanks that answers my question -- I'll use C# and .net instead.

Did you follow the link before coming to that conclusion?
The BigDate alternative is pretty easy.
 
B

Bjorn Abelli

Did you follow the link before coming to that conclusion?
The BigDate alternative is pretty easy.

I don't think that would suffice for the OP.

In the C# group he was practically served the complete code. Here we asked
him to think out the algorithm for himself... ;-)

But for the OP's problem, BigDate would have made it possible to use exactly
the same solution as he was given in the C# group, to instantiate a BigDate
at the epoch of a VB Date, and then to simply use the method addDays(int).

// Bjorn A



Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 
L

Lasse Reichstein Nielsen

Terry Jolly said:
In your example you're using 33841 (var theDate = new Date(1900,0,38841);)
which is what I'm trying to get from the Date. Maybe I'm mis-understanding
you.

No, it was me who was misunderstanding the direction of the problem.

The other direction is a little harder since you have to convert a difference in milliseconds to one in days.

Date theDate = new GregorianCalendar(2006, Calendar.MAY, 4).getTime();
Date epoch = new GregorianCalendar(1900, Calendar.JANUARY, 1).getTime();
int days = (int) Math.round((theDate.getTime() - epoch.getTime())/864E5) + 1;

Again it gives the correct number of days, so in the range 1900..2099, you
should add one to the number if 60 or above:

if (days >= 60) { days++; } // match Excel, which believes in 1900-02-29

/L
 
O

Oliver Wong

Bjorn Abelli said:
...

I don't think that would suffice for the OP.

In the C# group he was practically served the complete code. Here we asked
him to think out the algorithm for himself... ;-)

Also, nothing works better with Microsoft products than more Microsoft
products. That's one of the reasons they keep getting antitrust trials.

- Oliver
 
T

Terry Jolly

Many Thanks!


Lasse Reichstein Nielsen said:
No, it was me who was misunderstanding the direction of the problem.

The other direction is a little harder since you have to convert a
difference in milliseconds to one in days.

Date theDate = new GregorianCalendar(2006, Calendar.MAY, 4).getTime();
Date epoch = new GregorianCalendar(1900, Calendar.JANUARY, 1).getTime();
int days = (int) Math.round((theDate.getTime() - epoch.getTime())/864E5)
+ 1;

Again it gives the correct number of days, so in the range 1900..2099, you
should add one to the number if 60 or above:

if (days >= 60) { days++; } // match Excel, which believes in 1900-02-29

/L
--
Lasse Reichstein Nielsen - (e-mail address removed)
DHTML Death Colors:
<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
T

Terry Jolly

I intend to take a look at BigDate and have no idea what you mean refering
to me as OP. Yes, the C# group gave me the answer to which I am grateful.

First, I spent 5 years develop/maintaining an PMA system I wrote for the web
(using VB6 + asp) and I have about 60 companies using it.

Second, I have a full time job which itself is 50+ hours a week (and it's
not in IT). There's just not a lot of extra time left over. I spent the last
month reading books on Java and C# trying to determine the best approach for
moving my development.

Third, I would perfer to leave Microsoft behind - that's why I'm on this
newsgroup to begin with, however, I am not going multiple my efforts 10 fold
over simplest items (getting a darn date) just because I don't like
Microsoft.
 
B

Bjorn Abelli

...
I intend to take a look at BigDate and have no idea what you
mean refering to me as OP. Yes, the C# group gave me the
answer to which I am grateful.

Hi Terry,

"OP" is a common acronym in Usenet for "Original Poster",
i.e. the one starting a thread.

Glad to hear you have found a Java solution as well. :)

// Bjorn A



Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top