Simple Java naming trick: now and today

When browsing and reading code, I often find this:

Date currentDate = new Date();

Someone needs a current date. The line is unclear what the developer really wanted to achieve. Does he want the current date or the current time? The Date class in Java is ambiguous: "The class Date represents a specific instant in time, with millisecond precision. " and therefore should probably be called Time with Date only representing a day (the better named Time class resides in java.sql, ugh, one of the ugly decisions in Java).

Better name your variables with more meaning. Make your intentions clearer and use Calendar, which is more useful when working with dates[1]:

Calendar today = today();
Date now = now();

The implementation looks like this (use static imports) [2]:

public static Calendar today() {
	return Calendar.getInstance();

public static Date now() {
	return new Date();

Please, make your code more understandable for all the developers who follow you.

[1] You probably want to use Joda Time
[2] I know, Calendar stores the time also

Stephan Schmidt Administrator
CTO Coach , svese
Stephan is a CTO coach. He has been a coder since the early 80s, has founded several startups and worked in small and large companies as CTO. After he sold his latest startup he took up CTO coaching. He can be found on LinkedIn or follow him in Twitter.
follow me