Date & Time API (java.time) Quiz
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.
Question 1
What is the main advantage of java.time over legacy Date/Calendar?
Question 2
What does LocalDate represent?
Question 3
What does LocalTime represent?
Question 4
What is the difference between LocalDateTime and ZonedDateTime?
Question 5
What is the result of this LocalDate example?
LocalDate date = LocalDate.of(2023, 12, 25);
System.out.println(date.getDayOfWeek());Question 6
How do you format a LocalDateTime?
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formatted = now.format(formatter);
System.out.println(formatted);Question 7
How do you parse a date string?
String dateStr = "2023-12-25";
LocalDate date = LocalDate.parse(dateStr);
System.out.println(date);Question 8
What is the difference between Duration and Period?
Question 9
What is the result of this ZonedDateTime example?
ZonedDateTime zdt = ZonedDateTime.now(ZoneId.of("America/New_York"));
System.out.println(zdt.getZone());Question 10
How do you add days to a LocalDate?
LocalDate date = LocalDate.of(2023, 12, 25);
LocalDate newDate = date.plusDays(7);
System.out.println(newDate);Question 11
What is the output of this date comparison?
LocalDate date1 = LocalDate.of(2023, 12, 25);
LocalDate date2 = LocalDate.of(2023, 12, 24);
System.out.println(date1.isAfter(date2));Question 12
How do you get the current date and time?
LocalDateTime now = LocalDateTime.now();
ZonedDateTime zonedNow = ZonedDateTime.now();Question 13
What is the result of this Duration example?
Duration duration = Duration.ofHours(2);
System.out.println(duration.toMinutes());Question 14
What is the result of this Period example?
Period period = Period.ofMonths(1);
LocalDate date = LocalDate.of(2023, 1, 31);
LocalDate newDate = date.plus(period);
System.out.println(newDate);Question 15
How do you format a date with custom pattern?
LocalDate date = LocalDate.of(2023, 12, 25);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String formatted = date.format(formatter);
System.out.println(formatted);Question 16
What is the output of this parsing example?
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date = LocalDate.parse("25/12/2023", formatter);
System.out.println(date);Question 17
How do you convert between timezones?
ZonedDateTime nyTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
ZonedDateTime londonTime = nyTime.withZoneSameInstant(ZoneId.of("Europe/London"));
System.out.println(londonTime);Question 18
What is the result of this date arithmetic?
LocalDate date = LocalDate.of(2023, 2, 28);
LocalDate nextDay = date.plusDays(1);
System.out.println(nextDay);Question 19
How do you calculate the difference between two dates?
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");Question 20
What is the output of this time calculation?
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");Question 21
How do you handle daylight saving time transitions?
Question 22
What is the result of this formatting with locale?
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);Question 23
How do you create a date from year, month, day?
LocalDate date = LocalDate.of(2023, Month.DECEMBER, 25);
System.out.println(date);Question 24
What is the difference between Instant and ZonedDateTime?
Question 25
What is the output of this leap year check?
LocalDate date = LocalDate.of(2024, 2, 29);
System.out.println(date.isLeapYear());Question 26
How do you get the first day of the month?
LocalDate date = LocalDate.of(2023, 12, 25);
LocalDate firstDay = date.withDayOfMonth(1);
System.out.println(firstDay);Question 27
What is the result of this timezone conversion?
LocalDateTime local = LocalDateTime.of(2023, 12, 25, 12, 0);
ZonedDateTime zoned = local.atZone(ZoneId.of("UTC"));
System.out.println(zoned);Question 28
How do you parse a date with custom formatter?
String dateStr = "25-Dec-2023";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MMM-yyyy");
LocalDate date = LocalDate.parse(dateStr, formatter);
System.out.println(date);Question 29
What is the output of this duration calculation?
Duration twoHours = Duration.ofHours(2);
Duration thirtyMins = Duration.ofMinutes(30);
Duration total = twoHours.plus(thirtyMins);
System.out.println(total.toMinutes());Question 30
How do you check if a date is before another date?
LocalDate date1 = LocalDate.of(2023, 12, 25);
LocalDate date2 = LocalDate.of(2023, 12, 24);
System.out.println(date1.isBefore(date2));Question 31
What is the result of this period calculation?
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());Question 32
How do you format time with milliseconds?
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);Question 33
What is the output of this instant example?
Instant now = Instant.now();
System.out.println(now);Question 34
How do you get the last day of the month?
LocalDate date = LocalDate.of(2023, 12, 25);
LocalDate lastDay = date.withDayOfMonth(date.lengthOfMonth());
System.out.println(lastDay);Question 35
Mastering Java Date & Time API means you now write code that:
