Java date (java.util.date) with 4 examples

The date class of Java

Java comes up with a few date related classes to work with date and time in the Java projects. The Date class is one of those. The Date class is the part of java.util package.

This chapter about the Java Date explains how to use the Date class, its useful methods and also shows the examples of date formatting.

This is how you will include the date class in a Java project:

import java.util.Date;

So, you may simply include the java.util.Date rather than importing the whole package into your project.

A day, full month name and year example

The Date class is pretty simple to use and a better option if you only intend to work with the current date. However, if you need to work with formatting the dates e.g. displaying the date in mm/dd/yyyy or mm-dd-yyyy format then you have to use the Java SimpleDateFormat class (explained in its own chapter).

Java Date class constructors

The Date class supports two types of constructors:

  1. Date(); // that assigns current date and time to an object.
  2. Date (long millisSinceEpoch); // This constructor of date class takes one argument which is in milliseconds and equal to the time elapsed since Jan 1, 1970, 00:00:00 GMT.

Useful methods of the date Java class

Most of the methods of Java Date class are deprecated due to the lack of internationalization support. Following are useful methods of Date class.

1-      getTime();

The getTime() method returns the number of milliseconds since the epoch (Jan 1, 1970, 00:00:00 GMT) in the long format.

2-      toString()

Converts the given date object into the string in local time zone.

Example of Java Date to get current date

The example below simply imports the Date class. In the example, a date object is created and after that, we will print the current date.

Experience this online

The output will be:

Current Date: Sat Jan 15 18:48:38 PKT 2011

Date format using printf method

You can also use the printf method to format the dates quite easily. Java supports date and time conversion characters that can be used with the printf method to format dates. A list of those important characters is given below but first, let us show you how to use the printf with the date.

Getting current date and time with printf

To get the current date and time, the example below uses string format function. The ‘c’ character with %t gets the current date and time, see below:

Experience this online

The output will be:

Current date/time: Sat Jan 15 18:48:38 PKT 2011

Note that, the first Date object is created before using it with the printf method.

Java Current date in day, full month name, year format example

The following example displays the date in day, full month name and year format e.g. 13 March 2011, by using the Java Date and printf method. You have to import the java.util.date package.

Experience this online

The output will be:

Current date: 15 January, 2011

List of characters to format date in Java

Following is the list of characters that can be used in the string formatting and displayed with the printf method.

  • c = full date and time e.g. Sat Jan 15 2011 18:48:38 PKT.
  • d = two digit day with a leading zero.
  • e = day without leading zero.
  • A = name of the day, like Sunday, Monday.
  • a = short name of the day name like Sun, Mon.
  • B = Full Month name (October, June).
  • b = Short name of the Month (Jan, Feb).
  • m = Month number with leading 0 (01,02…)
  • Y  = Year in four digits e.g. 2000, 2010.
  • y  = Year with last two digits e.g. 01, 10.
  • D = US formatted date in mm/dd/yyyy format.

Example of Date formatting with SimpleDateFormat class

The example below shows how to format the date in Java. We will use the SimpleDateFormat class that is the part of the java.text package. The SimpleDateFormat is great for parsing and formatting the dates.

Experience this online

The output will be:

Current Date in dd/MM/yyyy format: 15/01/2011

Current Date in MM/dd/yyyy format: 01/15/2011

Current Date and time in MM/dd/yyyy format: 01/15/2011 06:07:45