Given a method that needs to use the values of the current day/month/year on every invocation and which is invoked several (millions or more) times per second, is there any faster way of implementing that functionality, other than creating a Calendar
object for the current time on every invocation?
Using Calendar.getInstance()
or calendar.setTime()
(with time field recalculation) is overly expensive to do many times per second.
Standard Java solutions are preferable (e.g., over using third party libraries).
To get the current date/time, I find that LocalDate.now()
is faster than Calendar.getInstance()
. The LocalDate class can be found in the java.time
package, which is new since Java 8 (Of course, if you want to maintain compatibility with old Java versions, that may not be an option).