devxlogo

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

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.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

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.

About Our Journalist