How can I create or set JotaTime DateTime objects from / to the maximum value?

I translated some C # code into Java and chose the JodaTime DateTime class to replace C# System.DateTime .

In C# the DateTime class has a field called MaxValue and one called MinValue , which returns the largest and smallest possible value that a DateTime object can contain.

I am trying to do the same with the JodaTime api. I read some suggestions on other posts.

This: Java equivalent of .NET DateTime.MinValue, DateTime.Today answers how to make today's date in JodaTime , but when answering the second half of the question about Min Values, they turn to Calendar and Date

Similarly, I saw proposals for passing the maximum long value as a constructor parameter, but it was criticized for being dependent on classes that might change in the future, and therefore they might be incompatible or exact after API updates.

So, is there one positively correct way to do this? If not, is there a good way to achieve this?

+6
source share
1 answer

Java 8 LocalDate has two meanings. LocalDate.MAX and LocalDate.MIN

LocalDate.MAX - The maximum supported LocalDate, '+ 999999999-12-31'. This can be used by the app as a โ€œfar futureโ€ date.

LocalDate.MIN - The minimum supported LocalDate, '-999999999-01-01'. This can be used by the application as the date of the "distant past."

Note: they do not go to Long.MIN_VALUE or Long.MAX_VALUE.


I suggest using Java 8 if you are transitioning from C # and how the date / time works, since it has an AND closure and a new DateTime API based on JodaTime. This new DateTime API is the one you should use if you are worried about the future of the API.

I think you can assume that Long.MIN_VALUE and Long.MAX_VALUE will never change, because they are based on determining how signed 64-bit values โ€‹โ€‹work. (How work with 64-bit values โ€‹โ€‹was standardized before you were born is likely). You can also assume that Date will not change because it has not changed much since its release, and since it has been replaced, there is even less reason to change it. Theoretically, this might be deprecated, but in fact there is still too much code that uses it.

IMHO, I use long to represent the time in milliseconds ala System.currentTimeMillis (), and I use Long.MIN_VALUE and Long.MAX_VALUE.

If you are worried about using a good API and future code verification, I suggest you avoid using Calendar. Not that everything was bad, but there is every reason to replace it.

+5
source

All Articles