Date & Time API (java.time) Quiz

Java
0 Passed
0% acceptance

35 in-depth questions (18 with code) covering LocalDate & LocalTime, LocalDateTime & ZonedDateTime, formatting dates, parsing dates, and Duration & Period in Java's modern date-time API.

35 Questions
~70 minutes
1

Question 1

What is the main advantage of java.time over legacy Date/Calendar?

A
Immutable, thread-safe classes with better API design
B
Faster performance
C
Smaller memory footprint
D
Backward compatibility
2

Question 2

What does LocalDate represent?

A
A date without time or timezone (year-month-day)
B
A date with time but no timezone
C
A date with timezone
D
A timestamp
3

Question 3

What does LocalTime represent?

A
A time without date or timezone (hours-minutes-seconds)
B
A time with date but no timezone
C
A time with timezone
D
A duration
4

Question 4

What is the difference between LocalDateTime and ZonedDateTime?

A
LocalDateTime has no timezone, ZonedDateTime includes timezone information
B
LocalDateTime is mutable, ZonedDateTime is immutable
C
ZonedDateTime is deprecated
D
They are identical
5

Question 5

What is the result of this LocalDate example?

java
LocalDate date = LocalDate.of(2023, 12, 25);
System.out.println(date.getDayOfWeek());
A
MONDAY
B
Compilation error
C
Runtime exception
D
No output
6

Question 6

How do you format a LocalDateTime?

java
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formatted = now.format(formatter);
System.out.println(formatted);
A
Using DateTimeFormatter with custom patterns
B
Using SimpleDateFormat
C
Using toString()
D
Using printf()
7

Question 7

How do you parse a date string?

java
String dateStr = "2023-12-25";
LocalDate date = LocalDate.parse(dateStr);
System.out.println(date);
A
Using parse() method with default ISO format
B
Using DateTimeFormatter
C
Using SimpleDateFormat
D
Using new Date()
8

Question 8

What is the difference between Duration and Period?

A
Duration measures time in seconds/nanoseconds, Period measures dates in years/months/days
B
Duration is for dates, Period is for time
C
Period is deprecated
D
They are identical
9

Question 9

What is the result of this ZonedDateTime example?

java
ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("America/New_York"));
System.out.println(zdt.getZone());
A
America/New_York
B
Compilation error
C
Runtime exception
D
No output
10

Question 10

How do you add days to a LocalDate?

java
LocalDate date = LocalDate.of(2023, 12, 25);
LocalDate newDate = date.plusDays(7);
System.out.println(newDate);
A
Using plusDays() method
B
Using add()
C
Using setTime()
D
Using Calendar.add()
11

Question 11

What is the output of this date comparison?

java
LocalDate date1 = LocalDate.of(2023, 12, 25);
LocalDate date2 = LocalDate.of(2023, 12, 24);
System.out.println(date1.isAfter(date2));
A
true
B
false
C
Compilation error
D
Runtime exception
12

Question 12

How do you get the current date and time?

java
LocalDateTime now = LocalDateTime.now();
ZonedDateTime zonedNow = ZonedDateTime.now();
A
Using now() static method
B
Using new Date()
C
Using Calendar.getInstance()
D
Using System.currentTimeMillis()
13

Question 13

What is the result of this Duration example?

java
Duration duration = Duration.ofHours(2);
System.out.println(duration.toMinutes());
A
120
B
2
C
Compilation error
D
Runtime exception
14

Question 14

What is the result of this Period example?

java
Period period = Period.ofMonths(1);
LocalDate date = LocalDate.of(2023, 1, 31);
LocalDate newDate = date.plus(period);
System.out.println(newDate);
A
2023-02-28
B
2023-03-03
C
Compilation error
D
Runtime exception
15

Question 15

How do you format a date with custom pattern?

java
LocalDate date = LocalDate.of(2023, 12, 25);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String formatted = date.format(formatter);
System.out.println(formatted);
A
25/12/2023
B
12/25/2023
C
Compilation error
D
Runtime exception
16

Question 16

What is the output of this parsing example?

java
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date = LocalDate.parse("25/12/2023", formatter);
System.out.println(date);
A
2023-12-25
B
Compilation error
C
Runtime exception
D
No output
17

Question 17

How do you convert between timezones?

java
ZonedDateTime nyTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
ZonedDateTime londonTime = nyTime.withZoneSameInstant(ZoneId.of("Europe/London"));
System.out.println(londonTime);
A
Using withZoneSameInstant() to convert while keeping the same instant
B
Using setTimeZone()
C
Using Calendar.setTimeZone()
D
Using TimeZone.convert()
18

Question 18

What is the result of this date arithmetic?

java
LocalDate date = LocalDate.of(2023, 2, 28);
LocalDate nextDay = date.plusDays(1);
System.out.println(nextDay);
A
2023-03-01
B
2023-02-29
C
Compilation error
D
Runtime exception
19

Question 19

How do you calculate the difference between two dates?

java
LocalDate start = LocalDate.of(2023, 1, 1);
LocalDate end = LocalDate.of(2023, 12, 31);
Period period = Period.between(start, end);
System.out.println(period.getYears() + " years, " + period.getMonths() + " months, " + period.getDays() + " days");
A
Using Period.between() for date differences
B
Using Duration.between()
C
Using ChronoUnit.between()
D
Using date1 - date2
20

Question 20

What is the output of this time calculation?

java
LocalTime start = LocalTime.of(9, 0);
LocalTime end = LocalTime.of(17, 30);
Duration duration = Duration.between(start, end);
System.out.println(duration.toHours() + " hours, " + duration.toMinutesPart() + " minutes");
A
8 hours, 30 minutes
B
8 hours, 0 minutes
C
Compilation error
D
Runtime exception
21

Question 21

How do you handle daylight saving time transitions?

A
ZonedDateTime automatically handles DST transitions correctly
B
Use LocalDateTime and manually adjust
C
DST is handled by the OS
D
Use Calendar for DST
22

Question 22

What is the result of this formatting with locale?

java
LocalDate date = LocalDate.of(2023, 12, 25);
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL).withLocale(Locale.FRANCE);
String formatted = date.format(formatter);
System.out.println(formatted);
A
lundi 25 décembre 2023
B
Monday, December 25, 2023
C
Compilation error
D
Runtime exception
23

Question 23

How do you create a date from year, month, day?

java
LocalDate date = LocalDate.of(2023, Month.DECEMBER, 25);
System.out.println(date);
A
Using LocalDate.of() with Month enum
B
Using new Date()
C
Using Calendar.set()
D
Using DateFormat.parse()
24

Question 24

What is the difference between Instant and ZonedDateTime?

A
Instant represents a moment in time (timestamp), ZonedDateTime includes timezone information
B
Instant is mutable, ZonedDateTime is immutable
C
ZonedDateTime is deprecated
D
They are identical
25

Question 25

What is the output of this leap year check?

java
LocalDate date = LocalDate.of(2024, 2, 29);
System.out.println(date.isLeapYear());
A
true
B
false
C
Compilation error
D
Runtime exception
26

Question 26

How do you get the first day of the month?

java
LocalDate date = LocalDate.of(2023, 12, 25);
LocalDate firstDay = date.withDayOfMonth(1);
System.out.println(firstDay);
A
Using withDayOfMonth(1)
B
Using setDay(1)
C
Using Calendar.set()
D
Using Date.setDate(1)
27

Question 27

What is the result of this timezone conversion?

java
LocalDateTime local = LocalDateTime.of(2023, 12, 25, 12, 0);
ZonedDateTime zoned = local.atZone(ZoneId.of("UTC"));
System.out.println(zoned);
A
2023-12-25T12:00Z[UTC]
B
Compilation error
C
Runtime exception
D
No output
28

Question 28

How do you parse a date with custom formatter?

java
String dateStr = "25-Dec-2023";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
LocalDate date = LocalDate.parse(dateStr, formatter);
System.out.println(date);
A
2023-12-25
B
Compilation error
C
Runtime exception
D
No output
29

Question 29

What is the output of this duration calculation?

java
Duration twoHours = Duration.ofHours(2);
Duration thirtyMins = Duration.ofMinutes(30);
Duration total = twoHours.plus(thirtyMins);
System.out.println(total.toMinutes());
A
150
B
120
C
Compilation error
D
Runtime exception
30

Question 30

How do you check if a date is before another date?

java
LocalDate date1 = LocalDate.of(2023, 12, 25);
LocalDate date2 = LocalDate.of(2023, 12, 24);
System.out.println(date1.isBefore(date2));
A
false
B
true
C
Compilation error
D
Runtime exception
31

Question 31

What is the result of this period calculation?

java
LocalDate birthDate = LocalDate.of(1990, 5, 15);
LocalDate today = LocalDate.of(2023, 12, 25);
Period age = Period.between(birthDate, today);
System.out.println(age.getYears());
A
33
B
34
C
Compilation error
D
Runtime exception
32

Question 32

How do you format time with milliseconds?

java
LocalTime time = LocalTime.of(14, 30, 45, 123456789);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");
String formatted = time.format(formatter);
System.out.println(formatted);
A
14:30:45.123
B
14:30:45.123456789
C
Compilation error
D
Runtime exception
33

Question 33

What is the output of this instant example?

java
Instant now = Instant.now();
System.out.println(now);
A
2023-12-25T12:00:00Z
B
Compilation error
C
Runtime exception
D
No output
34

Question 34

How do you get the last day of the month?

java
LocalDate date = LocalDate.of(2023, 12, 25);
LocalDate lastDay = date.withDayOfMonth(date.lengthOfMonth());
System.out.println(lastDay);
A
2023-12-31
B
2023-12-25
C
Compilation error
D
Runtime exception
35

Question 35

Mastering Java Date & Time API means you now write code that:

A
Handles dates, times, and timezones correctly with immutable classes, proper formatting, and accurate calculations that work across DST and locale boundaries
B
Uses legacy Date and Calendar classes
C
Ignores timezone issues
D
Always uses system default timezone

QUIZZES IN Java