How to get current time in java without Creating an Object each time?
calendar, java
Solution
You can also create a Calendar instance once and keep calling:
calendar.setTimeInMillis ( System.currentTimeMillis() );
to set the current time in calendar instance and use that calendar object to do any date calculation.
Problem
I know there are a lot os posts about how to get the current time in java. But im planning to code a getAge Method in a Persona class. The problem is, I need to get the current time each time method is called. Let's say a billion clients execute getMethod(with miliseconds, perhaps seconds of difference), a billion of objects will be created for such a simple thing. The only thing I did was create a member static in Person, so Person will share the instace. But this doesnt prevent the object creation. ``` public class Person{ //Some Attributes private static Calendar now; private Calendar birthDate; public short getAge(){ now = Calendar.getInstance(); return (short) (( now.getTimeInMillis() - birthDate.getTimeInMillis())/ 31536000000L; }} ``` If you know some library that have this implemented without the waste of such heap size. Please tell me. Thanks.