devxlogo

Use System.currentTimeMills(); Instead of new Date();

This tip is for when you want to place an action after a time gap between two occurrences (for example, to invalidate an unused session after some permitted time, or to note the total time use of a resource). Instead of storing the starting time of the first occurrence (event) and the ending time of next occurrence (event) as in block A, follow the procedure shown in block B.

 //block ADate start = new Date();//after some thingDate end  = new Date();//Do some thing with start and end to find the time difference //block Blong start = System.currentTimeMills();//after some thinglong end  = System.currentTimeMills();long timeUsed = end - start;//do something with timeUsed


The technique shown in block B avoids the unnecessary creation and garbage collection of two Objects, hence increasing performance.

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  How Engineering Leaders Spot Weak Proposals

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.