Drills in Java Dates and Times
Exercises to Get You Up To Speed Quickly
Introduction
This article can help you if you’re already familiar with Java and you need to:
- Prepare for interviews, or
- Reacquaint yourself with the language after a period away from Java e.g. moving from frontend to a backend project.
Each part consists of a “drill” that explores topics in Java.
Names of variables and methods are kept deliberately short to minimize the amount of typing required to complete the code exercises.
What This Article Is Not
This article is about learning by doing and doesn’t offer thorough explanations of the topics explored in the code drills. You’re left to explore these operations in more detail in your own time.
Setup
You can work with Java immediately using an online IDE such as Repl.
On the top left-hand side, click the Create button and search for Java in the Template dropdown list as shown below, then click Create Repl.
(You can also follow these instructions to install the latest version of Java on your computer, and download an IDE such as Eclipse or Netbeans).
Click the Run button — the console should print “Hello World”:
To make it quicker (and easier to read), print to the console using this method:
static void o(Object x) {
System.out.println(x);
}
To test this out, change the main method as follows to print the sum of two numbers:
public static void main(String[] args) {
o(2 + 2);
}
Next, copy and paste the following imports above the Main class:
import java.time.*;
import java.time.format.*;
import java.time.temporal.ChronoUnit;
Your code should look like this:
import java.time.*;
import java.time.format.*;
import java.time.temporal.ChronoUnit;class Main {
public static void main(String[] args) {
o(2 + 2);
}
static void o(Object x) {
System.out.println(x);
}
}
If the code compiles and outputs the number 4, we’re now ready to begin the drills!
Please note the following:
- The result of code execution is placed on the right after //.
- Code that throws an exception is marked with 🔥.
- Code that doesn’t compile is marked with ❌.
Basics
Date
o(LocalDate.now()); // 2022-10-06
o(LocalDate.of(2022, Month.JANUARY, 1)); // 2022-01-01
o(LocalDate.of(2022, 1, 1)); // 2022-01-01
o(LocalDate.of(1,9,32)); // 🔥
Time
o(LocalTime.now()); // 13:34:17.615228736
o(LocalTime.of(2,3)); // 02:03
o(LocalTime.of(2,3,4)); // 02:03:04
o(LocalTime.of(2,3,4,5)); // 02:03:04.000000005
DateTime
o(LocalDateTime.now());
// 2022-10-06T13:34:17.61565480o(LocalDateTime.of(2022,Month.JANUARY,1,2,3,4,5));
// 2022-01-01T02:03:04.000000005var d = LocalDate.of(2022,Month.JANUARY,1);
var t = LocalTime.of(2,3,4,5);
var dt = LocalDateTime.of(d, t);o(dt);
// 2022-01-01T02:03:04.000000005
Some Date Methods
Reusing the d variable from above:
var d = LocalDate.of(2022,Month.JANUARY,1);o(d.getMonth()); // "JANUARY"
o(d.getYear()); // 2022
o(d.getDayOfWeek()); // "SATURDAY"
o(d.getDayOfYear()); // 1
Zoning
Reusing d, t, and dt variables from above:
var d = LocalDate.of(2022,Month.JANUARY,1);
var t = LocalTime.of(2,3,4,5);
var dt = LocalDateTime.of(d, t);o(ZonedDateTime.now());
// 2022-10-06T13:34:17.620841485Z[GMT]var z = ZoneId.of("US/Pacific");
o(ZonedDateTime.of(2022, 1, 1, 2, 3, 4, 5, z));
// 2022-01-01T02:03:04.000000005-08:00[US/Pacific]o(ZonedDateTime.of(d, t, z));
2022-01-01T02:03:04.000000005-08:00[US/Pacific]o(ZonedDateTime.of(dt, z));
2022-01-01T02:03:04.000000005-08:00[US/Pacific]
Differences Between Two Dates (Using ChronoUnit)
Reusing the d and dt variable from above:
var d = LocalDate.of(2022,Month.JANUARY,1);
var dx = LocalDate.of(2022,Month.JANUARY,4);o(ChronoUnit.DAYS.between(d, dx)); // 3
o(ChronoUnit.HOURS.between(d, dx)); // 🔥
o(ChronoUnit.MINUTES.between(d, dx)); // 🔥
o(ChronoUnit.SECONDS.between(d, dx)); // 🔥var dt = LocalDateTime.of(2022,Month.JANUARY,1,2,3,4,5);
var dtx = LocalDateTime.of(2022,Month.JANUARY,4,20,30,40,50);o(ChronoUnit.DAYS.between(dt, dtx)); // 3
o(ChronoUnit.HOURS.between(dt, dtx)); // 90
o(ChronoUnit.MINUTES.between(dt, dtx)); // 5427
o(ChronoUnit.SECONDS.between(dt, dtx)); // 325656
Formatting and Parsing
Date Formatting
Reusing d, t, and dt variables from above:
var d = LocalDate.of(2022,Month.JANUARY,1);
var t = LocalTime.of(2,3,4,5);
var dt = LocalDateTime.of(d, t);o(d.format(DateTimeFormatter.ISO_LOCAL_DATE));
// 2022-01-01o(t.format(DateTimeFormatter.ISO_LOCAL_TIME));
// 02:03:04.000000005o(dt.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
// 2022-01-01T02:03:04.000000005var f1 = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
o(dt.format(f1)); // 01-01-2022 02:03:04var f2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);o(f2.format(d)); // 1/1/22
o(f2.format(t)); // 🔥
o(f2.format(dt)); // 1/1/22var f3 = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);o(f3.format(d)); // 🔥
o(f3.format(t)); // 2:03 AM
o(f3.format(dt)); // 2:03 AMvar f4 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);o(f4.format(d)); // 🔥
o(f4.format(t)); // 🔥
o(f4.format(dt)); // 1/1/22, 2:03 AM
Date and Time Parsing
var f6 = DateTimeFormatter.ofPattern("MM dd yyyy");
var f7 = DateTimeFormatter.ofPattern("MM dd yyyy HH:mm:ss");o(LocalDate.parse("01 02 2023", f6));
// 2023-01-02o(LocalTime.parse("12:01"));
// 12:01o(LocalDateTime.parse("01 02 2023 12:01:01", f7));
// 2023-01-02T12:01:01
Changing Dates and Times
Changing Dates
Reusing the d variable from above:
var d = LocalDate.of(2022,Month.JANUARY,1);o(d.plusDays(1)); // 2022-01-02
o(d.plusWeeks(1)); // 2022-01-08
o(d.plusMonths(1)); // 2022-02-01
o(d.plusYears(1)); // 2023-01-01o(d.minusDays(1)); // 2021-12-31
o(d.minusWeeks(1)); // 2021-12-25
o(d.minusMonths(1)); // 2021-12-01
o(d.minusYears(1)); // 2021-01-01o(d); // 2022-01-01 (careful!)d = d.plusDays(1);
o(d); // 2022-01-02
Changing Dates and Times
Reusing the dt variable from above:
var dt = LocalDateTime.of(2022,Month.JANUARY,1,2,3,4,5);o(dt.plusDays(1)); // 2022-01-02T02:03:04.000000005
o(dt.plusHours(1)); // 2022-01-01T03:03:04.000000005
o(dt.plusMinutes(1)); // 2022-01-01T02:04:04.000000005o(dt.minusDays(1)); // 2021-12-31T02:03:04.000000005
o(dt.minusHours(1)); // 2022-01-01T01:03:04.000000005
o(dt.minusMinutes(1)); // 2022-01-01T02:02:04.000000005o(dt
.plusDays(1)
.plusHours(1)
.plusMinutes(1)); // 2022-01-02T03:04:04.000000005
Periods, Durations, and Instants in Time
Periods
Reusing the d variable from above:
var d = LocalDate.of(2022,Month.JANUARY,1);var p1 = Period.ofDays(1);
var p2 = Period.ofWeeks(1);
var p3 = Period.ofMonths(1);
var p4 = Period.of(2,3,4);o(p1); // "P1D"
o(p2); // "P7D"
o(p3); // "P1M"
o(p4); // "P2Y3M4D"o(d.plus(p1)); // 2022-01-02
o(d.minus(p1)); // 2021-12-31
Durations (Smaller Time Periods)
Reusing the dt variable from above:
var dt = LocalDateTime.of(2022,Month.JANUARY,1,2,3,4,5);var d1 = Duration.ofDays(1);
var d2 = Duration.ofHours(1);
var d3 = Duration.ofMinutes(1);
var d4 = Duration.ofSeconds(1);
var d10 = Duration.of(10, ChronoUnit.SECONDS);o(d1); // PT24H
o(d2); // PT1H
o(d3); // PT1M
o(d4); // PT1S
o(d10); // PT10So(dt.plus(d1)); // 2022-01-02T02:03:04.000000005
o(dt.minus(d1)); // 2021-12-31T02:03:04.000000005
Instants
Reusing the dt variable from above:
var dt = LocalDateTime.of(2022,Month.JANUARY,1,2,3,4,5);
var z = ZoneId.of("US/Pacific");
var zdt = ZonedDateTime.of(dt, z);
var i = zdt.toInstant();o(zdt);
// 2022-01-01T02:03:04.000000005-08:00[US/Pacific]o(i);
// 2022-01-01T10:03:04.000000005Zo(Instant.ofEpochSecond(1641002584));
// 2022-01-01T02:03:04Zo(i.plus(10, ChronoUnit.SECONDS));
// 2022-01-01T10:03:14.000000005Zo(i.minus(10, ChronoUnit.HOURS));
// 2022-01-01T00:03:04.000000005Z
Thanks for reading! Let me know what you think in the comments section below, and don’t forget to subscribe. 👍