Selenium + Java · Lesson 13 of 44

0/44 done

Module 13: Java Date & Time

13. Modern Java Date & Time API for Test Scenarios

14 minlesson

Your 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.

Write it in the editor, then press Check answer (Ctrl + Enter).

Modern java.time for Test Engineers

java
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"));

Key Takeaways & Walkthrough Summary

Every test suite deals with timestamps: generating unique user emails, picking flight reservation dates, and verifying invoices. In this lesson, we use java.time to format dates and calculate future test dates. Let's master Date and Time!

Part of the Selenium + Java course (Selenium with Java). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why avoid the legacy java.util.Date and SimpleDateFormat?

SimpleDateFormat is not thread-safe and causes race conditions in parallel test runs. java.time classes are immutable and thread-safe.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Java
Skip