13. Modern Java Date & Time API for Test Scenarios

Modern java.time for Test Engineers

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"));
String uniqueEmail = "test_user_" + timestamp + "@targetqa.com";

LocalDate checkInDate = LocalDate.now().plusDays(7);
String formattedCheckIn = checkInDate.format(DateTimeFormatter.ofPattern("MM/dd/yyyy"));

Task

  • Implement DateUtil:
  • 1. Create a method getFutureDateFormatted(int plusDays, String pattern).
  • 2. Add plusDays to LocalDate.now() and format using DateTimeFormatter.ofPattern(pattern).
  • 3. Return the formatted date string.
πŸ’‘Common doubts
Did you like the problem?
Lesson 13 of 38
Java
Loading code editor…
Next