21

what is the range for valid values that I can store in java.util.Date? The API doesn't say much about this.

Or does it only support dates that can be expressed as unix timestamps (that is dates after 1.1.1970)? If so, is there maybe a (serializeable) class in the JDK that supports also dates prior to that?

What I'm looking for is a class/type for a birthday-field in db4o

Dexter
  • 3,072
  • 5
  • 31
  • 32

3 Answers3

29

It supports dates between Long.MIN_VALUE and Long.MAX_VALUE:

class DateTest {
    public static void main(String[] args) {
        DateFormat df = new SimpleDateFormat("d MMM yyyy G, HH:mm:ss.S Z");

        System.out.println(df.format(new Date(Long.MIN_VALUE)));
        System.out.println(df.format(new Date(0)));
        System.out.println(df.format(new Date(Long.MAX_VALUE)));
    }
}

Outputs

2 Dec 292269055 BC, 10:47:04.192 -0600
31 Dec 1969 AD, 18:00:00.0 -0600
17 Aug 292278994 AD, 01:12:55.807 -0600

(Note: times above are Central Time)

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
  • @Michael Borgwardt - Although `Date` itself doesn't have an era field (at least not an accessible one) - if I were to wrap it in a `Calendar`, it could be printed. – Rob Hruska Mar 30 '11 at 15:24
  • Guess what Date.toString() does (after guessing what System.println(Object) does). – Michael Borgwardt Mar 30 '11 at 15:26
  • @Michael Borgwardt - `Date#toString()` does its own fun little concatenation to build the outputted string, and it doesn't seem to have any case where it would print the era designator. Updated my example to use a `DateFormat`. – Rob Hruska Mar 30 '11 at 15:51
  • @Rob: Hm, I could swear I've seen a version of Date that delegated all of the date arithmetic to java.util.Calendar, but now it looks like they're both based on some sun.util.* classes. – Michael Borgwardt Mar 30 '11 at 16:02
  • @Michael - Just for kicks I looked at the source for 7 (I was looking at 6 earlier), and 7 is also backed by the Sun implementations. Maybe those are wrappers for `java.util.Calendar` in 7? I didn't go that far into the source. – Rob Hruska Mar 30 '11 at 16:46
  • any ideas why `java.sql.Timestamp` doesn't behave the same way in spite of being based on `java.util.Date`? – H.B. Jun 18 '23 at 14:55
15

java.util.Date stores dates in a long as milliseconds using 1970-01-01 as a reference. Since long is a signed 64-bit integer, you can expect java.util.Date to cover about 290 million years before and after the reference date - that is if you don't care about accurate representation and calendar system switches.

Unless you are planning a birthday party for a dinosaur, I'd say that java.util.Date is probably fine for your purpose...

thkala
  • 84,049
  • 23
  • 157
  • 201
  • 1
    That's helpful, thanks. I didn't know negative values were valid for timestamps. – Dexter Mar 30 '11 at 15:06
  • java.util.Date covers more than 292 million years BC while dinosaurs first appeared between 247 and 240 million years ago. So planning a birthday party for a dinosaur shouldn't be an issue. – Positive Navid Jun 18 '20 at 05:18
1

Dates can contain values before 1.1.1970, just use negative long :-)

Riccardo Cossu
  • 2,699
  • 1
  • 28
  • 47