O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Finding Today’s Date

Date today = new java.util.Date();
											System.out.println("Today's Date is " +
											today.toString());

The Date object in the java.util package is a class that you should be familiar with if your program deals with dates and times, as it is commonly used. Getting the current time and date is a very simple task. When you create an instance of the Date object, it is initialized with the current time and date.

An alternative method to getting the current date and time is to use the Calendar class. The following code will also get you the current date and time:

Calendar cal = Calendar.getInstance();

This will produce a Calendar object, cal, initialized with the current date and time.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required