Java date – java.util.date and other classes to work with dates

The date class

Java comes up with a few date related classes to work with date and time in the Java projects. The Date Java 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;

A current date example

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

A day, full month name 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 on formatting the dates e.g. displaying mm/dd/yyyy or mm-dd-yyyy formatted dates 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 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 date 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 printf with the date.

Getting current date and time with printf

To get 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, 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. In the real time applications, you have to format dates as per need like date format in dd/mm/yyyy or mm/dd/yyyy or Month, day, year etc. to make the presentation of dates more readable and understandable to the users of application. 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

 

See more detail and examples about Java SimpleDateFormat

Was this article helpful?

Related Articles

Leave A Comment?