Java Dates and Times

As previously stated, dates and times in Java can be stored in a ‘LocalDateTime’ variable. In its simplest form the current date and time can be assigned to a variable as follows. In order for this to work, ‘java.time.LocalDateTime’ needs to be imported.

LocalDateTime dateAndTime = LocalDateTime.now();

It is also possible to deal with dates and times separately, with the help of the import statements, ‘java.time.LocalDate’ and ‘java.time.LocalTime’.

LocalDate dateOnly = LocalDate.now();
LocalTime timeOnly = LocalTime.now();

Here, the current date and time are assigned to separate variables. Similarly, specific dates and times can be assigned.

LocalDateTime dateAndTime = LocalDateTime.of(2017, 2, 1, 5, 30, 0);
LocalDate dateOnly = LocalDate.of(2017, 2, 1);
LocalTime timeOnly = LocalTime.of(5, 30, 0);

For the ‘dateAndTime’ variable above, the date, 1 February 2017 is assigned, along with a time of 5:30am. The values in parenthesis refer to the year, month, day, hours, minutes and seconds. The ‘dateOnly’ variable is assigned the date, 1 February 2017, whilst ‘timeOnly’ is assigned, 5:30am.

If no formatting is applied, these variables can be displayed, for example, in the console, in a similar manner to other variables, such as strings, as shown below, using the ‘println’ method, along with the ‘toString’ method of the variables themselves.

System.out.println(dateAndTime.toString());
System.out.println(dateOnly.toString());
System.out.println(timeOnly.toString());

This will display in the console as follows.

2017-02-01T05:30
2017-02-01
05:30

Often, particularly with dates, this format is not ideal. Java allows for a number of different formats to be used. So that this formatting can take place, an additional import is needed, ‘java.time.format.DateTimeFormatter’. This allows a ‘DateTimeFormatter’ to be used to dictate how the dates and times are displayed by using a format pattern. The ‘format’ method is used instead of ‘toString’, so that a ‘DateTimeFormatter’ can be applied.

DateTimeFormatter formatDateAndTime =
	  DateTimeFormatter.ofPattern("EEEE, dd MMMM yyyy HH:mm");
DateTimeFormatter formatDateOnly =
	  DateTimeFormatter.ofPattern("EEE, dd-MMM-yyyy");
DateTimeFormatter formatTimeOnly = DateTimeFormatter.ofPattern("h:mma");

System.out.println(dateAndTime.format(formatDateAndTime));
System.out.println(dateOnly.format(formatDateOnly));
System.out.println(timeOnly.format(formatTimeOnly));

The resulting output from the above is shown below.

Wednesday, 01 February 2017 05:30
Wed, 01-Feb-2017
5:30am

The following table shows the different format patterns that can be used when displaying dates and times.

Format
Pattern
Description
G Era designator e.g. AD
yyyy Four digit year e.g. 2017
yy Two digit year e.g. 17
M Month as a number, with no leading zero e.g. 3 for March.
MM Month as a number, with a leading zero e.g. 03 for March.
MMM Three character abbreviation for the month e.g. Mar for March.
MMMM Full name for the month e.g. March.
d Day of the month as a number from 1 through 31.
dd Day of the month as a number from 01 through 31.
h Hours, using a 12 hour clock, with no leading zero e.g. 5.
hh Hours, using a 12 hour clock, with a leading zero e.g. 05.
H Hours, using a 24 hour clock, with no leading zero e.g. 17.
HH Hours, using a 24 hour clock, with a leading zero e.g. 20.
m Minutes, with no leading zero e.g. 6.
mm Minutes, with a leading zero e.g. 06.
s Seconds, with no leading zero e.g. 8.
ss Seconds, with a leading zero e.g. 08.
S Milliseconds e.g. 234
EEE Three character abbreviation for the day of the week e.g. Mon.
EEEE Full name for the day of the week e.g. Monday.
D Day in year as a number e.g. 360
F Day of week in month as a number e.g. 2.
w Week in year as a number e.g. 20.
W Week in month as a number e.g. 2.
a Time of day e.g. AM or PM.
k Hour in a day, as a number, from 1 through 24.
K Hour in a day, as a number, from 0 through 11.

As well as being able to format dates and times using the format patterns above, Java also provides a number of methods to access individual components of a date and time, as shown below.

LocalDateTime dateAndTime = LocalDateTime.of(2017, 2, 1, 5, 30, 0);

System.out.println(dateAndTime.getDayOfMonth());
System.out.println(dateAndTime.getMonth());
System.out.println(dateAndTime.getYear());
System.out.println(dateAndTime.getHour());
System.out.println(dateAndTime.getMinute());
System.out.println(dateAndTime.getSecond());

These also work for the date and time only variables.

On top of these, there are built in methods for manipulating dates and times, as can be seen below, where one is added to each component of a date and time.

LocalDateTime dateAndTime = LocalDateTime.of(2017, 2, 1, 5, 30, 0);

DateTimeFormatter formatDateAndTime =
	  DateTimeFormatter.ofPattern("dd MMMM yyyy HH:mm");

System.out.println(dateAndTime.format(formatDateAndTime));

dateAndTime = dateAndTime.plusDays(1);
dateAndTime = dateAndTime.plusMonths(1);
dateAndTime = dateAndTime.plusYears(1);
dateAndTime = dateAndTime.plusHours(1);
dateAndTime = dateAndTime.plusMinutes(1);
dateAndTime = dateAndTime.plusSeconds(1);

System.out.println(dateAndTime.format(formatDateAndTime));

The above will display the original date and time, followed by the date and time with one added to each component.

01 February 2017 05:30
02 March 2018 06:31

In order to subtract from the various components of a date and time, there are corresponding ‘minus’ methods, such as ‘minusDays’, ‘minusMonths’ and so on.

The methods discussed above are by no means exhaustive. There are many others available.