AlgoMaster Logo

Date & Time API - Quiz

Last Updated: December 6, 2025

1 min read

Date & Time API Exercises

31 quizzes

1
Code Completion

Create a LocalDate for 2023-10-01 to represent a due date in a task manager

java
1
LocalDate dueDate = LocalDate.(2023, 10, 1);

Click an option to fill the blank:

2
Code Completion

Measure how many days are between two LocalDate values for a subscription period

java
1
long days = ChronoUnit. .between(startDate, endDate);

Click an option to fill the blank:

3
Code Completion

Format a LocalDateTime using a reusable formatter before logging it

java
1
String text = dateTime.format(DateTimeFormatter. );

Click an option to fill the blank:

4
Multiple Choice

Which statement about the Java 8 Date & Time API classes like LocalDate and LocalTime is TRUE?

5
Multiple Choice

What does LocalDate represent in the Java Date & Time API?

6
Multiple Choice

Which method gets the current LocalTime using the system clock?

7
Multiple Choice

Which class should you use to represent a moment on the UTC timeline for logging events?

8
Multiple Choice

You need to schedule a meeting for "2025-03-10T09:00" in Europe/Berlin. Which class best represents this?

9
Multiple Choice

Which type is best for representing a 3-day vacation length, independent of start date?

10
Multiple Choice

Which DateTimeFormatter is predefined for a standard ISO local date like 2025-01-31?

11
Multiple Choice

How do DateTimeFormatter patterns treat 'MM' and 'mm'?

12
Multiple Choice

What does ChronoUnit.DAYS.between(start, end) return when used with two LocalDate values?

13
Multiple Choice

Which approach converts a LocalDateTime to a specific time zone-aware value?

14
Multiple Choice

Which statement about Duration and Period is TRUE?

15
Sequencing

Order the steps to create a ZonedDateTime from the current local date-time in a specific zone and then format it for display.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to measure a task's running time using Instant and Duration.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code that calculates days between two dates?

1import java.time.*;
2import java.time.temporal.ChronoUnit;
3
4public class Main {
5    public static void main(String[] args) {
6        LocalDate start = LocalDate.of(2024, 1, 1);
7        LocalDate end = LocalDate.of(2024, 1, 10);
8        long days = ChronoUnit.DAYS.between(start, end);
9        System.out.println(days);
10    }
11}
18
Output Prediction

What does this code print when adding a Period to a LocalDate?

1import java.time.*;
2
3public class Main {
4    public static void main(String[] args) {
5        LocalDate date = LocalDate.of(2023, 12, 31);
6        Period period = Period.ofDays(1);
7        LocalDate result = date.plus(period);
8        System.out.println(result);
9    }
10}
19
Output Prediction

What is the output when formatting a LocalTime?

1import java.time.*;
2import java.time.format.*;
3
4public class Main {
5    public static void main(String[] args) {
6        LocalTime time = LocalTime.of(9, 5);
7        DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:mm");
8        System.out.println(time.format(f));
9    }
10}
20
Output Prediction

What does this code print when converting a LocalDateTime to an Instant in UTC?

1import java.time.*;
2
3public class Main {
4    public static void main(String[] args) {
5        LocalDateTime ldt = LocalDateTime.of(2020, 1, 1, 0, 0);
6        ZonedDateTime zdt = ldt.atZone(ZoneId.of("UTC"));
7        Instant instant = zdt.toInstant();
8        System.out.println(instant); 
9    }
10}
21
Output Prediction

What is printed when computing a Duration in minutes between two times?

1import java.time.*;
2
3public class Main {
4    public static void main(String[] args) {
5        LocalTime start = LocalTime.of(10, 0);
6        LocalTime end = LocalTime.of(11, 30);
7        long minutes = Duration.between(start, end).toMinutes();
8        System.out.println(minutes);
9    }
10}
22
Bug Spotting

Find the bug in this code that parses a date string and prints it.

Click on the line(s) that contain the bug.

java
1
import java.time.*;
2
import java.time.format.*;
3
 
4
public class Main {
5
    public static void main(String[] args) {
6
        String text = "31-01-2024";
7
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
8
        LocalDate date = LocalDate.parse(text, formatter);
9
        System.out.println(date);
10
    }
11
}
23
Bug Spotting

Find the bug in this code that tries to create a Duration between two LocalDate values.

Click on the line(s) that contain the bug.

java
1
import java.time.*;
2
 
3
public class Main {
4
    public static void main(String[] args) {
5
        LocalDate start = LocalDate.of(2024, 1, 1);
6
        LocalDate end = LocalDate.of(2024, 1, 5);
7
        Duration d = Duration.between(start, end);
8
        System.out.println(d.toDays());
9
    }
10
}
24
Matching

Match each date-time class with its most appropriate description.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match the Date & Time API concepts with their typical use cases.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the code to create a LocalDateTime from separate LocalDate and LocalTime objects and format it using a custom pattern.

java
1
import java.time.*;
2
import java.time.format.*;
3
4
public class Main {
5
public static void main(String[] args) {
6
LocalDate date = LocalDate.of(2024, 5, 10);
7
LocalTime time = LocalTime.of(14, 30);
8
LocalDateTime dateTime = .of(date, time);
9
DateTimeFormatter formatter = DateTimeFormatter.("yyyy/MM/dd HH:mm");
10
String text = dateTime.(formatter);
11
System.out.println(text);
12
}
13
}

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code to add a one-week Period to a LocalDate and print the result.

java
1
import java.time.*;
2
3
public class Main {
4
public static void main(String[] args) {
5
LocalDate start = LocalDate.of(2024, 3, 1);
6
Period p = Period.(0, 0, 7);
7
LocalDate result = start.(p);
8
System.out.println(result);
9
}
10
}

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to obtain the current time in a specific zone and convert it to another zone.

java
1
import java.time.*;
2
3
public class Main {
4
public static void main(String[] args) {
5
ZonedDateTime nyTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
6
ZonedDateTime tokyoTime = nyTime.(ZoneId.of("Asia/Tokyo"));
7
Instant instant = tokyoTime.();
8
System.out.println(instant);
9
}
10
}

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the code to parse a date string with ISO_LOCAL_DATE and then print the next day.

java
1
import java.time.*;
2
import java.time.format.*;
3
4
public class Main {
5
public static void main(String[] args) {
6
String text = "2024-10-01";
7
LocalDate date = LocalDate.(text, DateTimeFormatter.ISO_LOCAL_DATE);
8
LocalDate nextDay = date.Days(1);
9
System.out.println(nextDay);
10
}
11
}

Click an option to fill blank 1:

30
Hotspot Selection

Click the line that applies the DateTimeFormatter to format the LocalDate.

Click on the line to select.

java
1
import java.time.*;
2
import java.time.format.*;
3
 
4
public class Main {
5
    public static void main(String[] args) {
6
        LocalDate today = LocalDate.now();
7
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
8
        String text = today.format(formatter);
9
        System.out.println(text);
10
    }
11
}
31
Hotspot Selection

Click the line that converts the current moment into a ZonedDateTime using the system default zone.

Click on the line to select.

java
1
import java.time.*;
2
 
3
public class Main {
4
    public static void main(String[] args) {
5
        Instant now = Instant.now();
6
        ZoneId systemZone = ZoneId.systemDefault();
7
        ZonedDateTime zoned = now.atZone(systemZone);
8
        System.out.println(zoned);
9
    }
10
}

Premium Content

Subscribe to unlock full access to this content and more premium articles.