Rudimentary problem(?) with built-in class...

Hi, I have been playing with the Date class of the java.util package and hit a very fundamental brick wall.

The class, as you know, records the time in milliseconds from the "epoch" (January 1st, 1970). The current time therefore is approx 1*10**12.

Because I am new to the Java world I thought I must just try something simple and set myself the problem of finding out what the date was at exactly a thousand billion milli-seconds from the epoch... but I can't.

java.util.Date.setDate takes a long integer variable. On my system, and Intel Mac, the java long is a signed 32bit number. It means if I try to set the current day to anything greater than 2147483647 ticks the compler throws a number too large error. This is a problem, because 2147483647 milliseconds after Jan 1st 1970 doesn't even get you to the end of the month! With a signed 32bit long you can set no date later than 25th January 1970.

So where's the bug? You could ask, why use Date when I could be using GregorianCalendar? And the answer is simply that the date methods I'm using are not deprecated.

I don't know if my long ints would be longer than 32 if I was using a different platform. I don't know if I can set the length in my compiler. All the literature I have seen tells me a Java long is 64bits but on this Intel Mac it isn't. Here is a simple source code example:

import java.util.Date;

publicclass date{

publicstaticvoid main(String[] args)

{

// Construct new Date object.

Date date =new Date();

// Display current date to console in both milli-ticks and standard format.

System.out.println(date.getTime());

System.out.println(date);

// Make a variable for time scalar.

long time;

// Try setting date to smallest long int and display.

time = -2147483648;

date.setTime(time);

System.out.println(date);

// Try setting date to largest long int and display.

time = 2147483647;

date.setTime(time);

System.out.println(date);

}

}

Obviously a date that can only be set to between 7th December 1969 and Jan 25th 1970 isn't much use. So have I made a mistake or should the entire Date class be deprecated?

What confuses me is that if I let the system just give me the current date, it's fine.

PS. Any tips of my coding style would be greatly appreciated. I've tried writing bigger programs and things get messy!

[3004 byte] By [vapourmilea] at [2007-10-3 3:32:40]
# 1

> java.util.Date.setDate takes a long integer variable.

> On my system, and Intel Mac, the java long is a

> signed 32bit number.

Longs are 64 bit in Java. On any platform.

> It means if I try to set the

> current day to anything greater than 2147483647 ticks

> the compler throws a number too large error.

That's because a number you enter is always an int, unless marked otherwise. As described by the JLS.

Try:

3147483647L

with L for long.

CeciNEstPasUnProgrammeura at 2007-7-14 21:26:57 > top of Java-index,Java Essentials,New To Java...
# 2
As for coding style: stick with the naming conventions, always use a package, don't name classes like standard classes and get rid of unnecessary comments like "// Make a variable for time scalar" - everybody can see that you're declaring a variable named time.
CeciNEstPasUnProgrammeura at 2007-7-14 21:26:57 > top of Java-index,Java Essentials,New To Java...
# 3
An exemplorary answer, thank you very much! I now know another essential fact about Java.Edit: PS. What do you mean (by example) by "always use packages" and "don't name classes like standard classes"?Message was edited by: vapourmile
vapourmilea at 2007-7-14 21:26:57 > top of Java-index,Java Essentials,New To Java...