Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valid range for java.util.Date?

Tags:

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

like image 741
Dexter Avatar asked Mar 30 '11 14:03

Dexter


People also ask

How do you validate a date range in Java?

SimpleDateFormat df = new SimpleDateFormat("dd:MM:yyyy"); df. setLenient(false); Date date = df. parse(dateRelease); Then it will throw ParseException when the date is not in a valid range.

What is date format of Java Util date?

Date format conversion yyyy-mm-dd to mm-dd-yyyy.

How do you validate a date in YYYY MM DD format in Java?

DateValidator validator = new DateValidatorUsingDateFormat("MM/dd/yyyy"); assertTrue(validator. isValid("02/28/2019")); assertFalse(validator. isValid("02/30/2019"));

How do I create a new Java Util date?

You can create a Date object using the Date() constructor of java. util. Date constructor as shown in the following example. The object created using this constructor represents the current time.


2 Answers

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)

like image 123
Rob Hruska Avatar answered Oct 03 '22 07:10

Rob Hruska


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

like image 43
thkala Avatar answered Oct 03 '22 07:10

thkala