Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When will the java date collapse?

Tags:

java

datetime

AFAIK java stores dates in long variables as milliseconds. Consequently someday there will be no value (cause long has a maximum) which will correspond to the time of that instant. Do you know when it will happen?

like image 444
Denys S. Avatar asked Nov 16 '10 14:11

Denys S.


People also ask

Is Java time deprecated?

Deprecated. This method is deprecated and should not be used because SQL TIME values do not have a month component. Parameters: i - the month value between 0-11.

Is date mutable in Java?

Prior to Java 8, the date classes are mutable. When we are using it as part of multi-threaded environments, developers has to make sure the thread safety of date objects. The Java 8 Date and Time API provides all the immutable classes which are thread safe. Developers are free of concurrency issues.

Is there a date datatype in Java?

The Date in Java is not only a data type, like int or float, but a class. This means it has its own methods available for use. A Date in Java also includes the time, the year, the name of the day of the week, and the time zone.

Does Java Util date have time?

No time data is kept. In fact, the date is stored as milliseconds since the 1st of January 1970 00:00:00 GMT and the time part is normalized, i.e. set to zero. Basically, it's a wrapper around java. util.


2 Answers

It's easy enough to find out:

public class Test {
    public static void main(String[] args) {
        System.out.println(new java.util.Date(Long.MAX_VALUE));
    }
}

Gives output (on my box):

Sun Aug 17 07:12:55 GMT 292278994

You may need to subtract a bit from Long.MAX_VALUE to cope with your time zone overflowing the range of long, but it will give a reasonable ballpark :)

like image 64
Jon Skeet Avatar answered Sep 19 '22 13:09

Jon Skeet


According to the current leap-year regulations the average number of days per year will be

         365 + 1/4 − 1/100 + 1/400 = 365.2425 days per year

This means that we, in average, have 31556952000 milliseconds per year.

The long-value represents the number of milliseconds since the Epoch (1st of January, 1970) and the maximum number represented by a Java long is 263 − 1, so the following calculation

         1970 + (263 − 1) / 31556952000

reveals that this representation will overflow year 292278994.


This can, as Jon Skeet points out, be confirmed by

-> System.out.println(new Date(Long.MAX_VALUE));
Sun Aug 17 08:12:55 CET 292278994
like image 31
aioobe Avatar answered Sep 20 '22 13:09

aioobe