Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to create a java.sql.Date for December 31, 9999 (UTC)?

Tags:

java

date

I would like to create a java.sql.Date for December 31, 9999 (UTC). Currently I'm using this:

Date eot = new Date(new GregorianCalendar(9999, Calendar.DECEMBER, 31).getTimeInMillis());

However, it strikes me as ugly. Is there a simpler way?

like image 244
ktm5124 Avatar asked Dec 07 '25 04:12

ktm5124


2 Answers

Why not just use a constant?

Date eot = new Date(253402214400000L);

Also note, the docs say that 8099-12-31 is the latest date supported. That would be 193444070400000L. Though in testing, it seems to work with 9999-12-31 just fine.

Also, I'll just point out you should probably avoid this API and use the java.time APIs instead.

like image 103
Matt Johnson-Pint Avatar answered Dec 08 '25 19:12

Matt Johnson-Pint


Use Java's new LocalDate class. If you must have a java.sql.Date, you can convert it with valueOf():

Date eot = Date.valueOf(LocalDate.of(9999, Month.DECEMBER, 31));
like image 37
shmosel Avatar answered Dec 08 '25 18:12

shmosel