Java Fundamentals

Modern Date/Time API and String Manipulation

Lecture 7

java.time package, LocalDate, LocalDateTime, DateTimeFormatter, and String APIs

Goal of this lecture

This lecture will help in the realization of your project, we will see how to :
  • Keep our data, even if the application is closed (File approach)
  • Organize textual information thanks to the String APIs
  • Manage collections of object
  • Setup a configuration for the application and choose an appropriate structure to store it

Record data in a file, usual need

A recurrent need in all programs is to perform technical traces, while the program is running
Those traces can be output in several format :
  • The console format
  • The file format
  • The data base format
This is featured is usually called Logging

What a Log class should do ?

A Log class (or a Logger) should output messages giving precise information on what is currently happening in the program
Regarding a particular event, the following information should be available
  • A precise temporal data indicating when the event has occurred
  • The message representing the event
  • If an error occurred, the location of that error could be a great indication

1st part: The Modern Date/Time API (java.time)

Java 8 introduced a comprehensive Date/Time API in the java.time package
  • Replaces the old java.util.Date and Calendar classes (which are deprecated)
  • Immutable and thread-safe by design
  • Clear separation between dates, times, and timestamps
  • Much easier to use and understand

Key Classes in java.time

The main classes you'll work with:
  • LocalDate - Date without time (e.g., 2025-10-06)
  • LocalTime - Time without date (e.g., 14:30:00)
  • LocalDateTime - Date and time without timezone (e.g., 2025-10-06T14:30:00)
  • ZonedDateTime - Date and time with timezone
  • Instant - Timestamp (point on timeline in UTC)
  • Period - Amount of time in years, months, days
  • Duration - Amount of time in hours, minutes, seconds

Working with LocalDate

Creating and manipulating dates without time:
package fr.tbr.exercises.example;

import java.time.LocalDate;
import java.time.Month;

public class LocalDateExample {
    public static void main(String[] args) {
        // Get current date
        LocalDate today = LocalDate.now();
        System.out.println("Today: " + today);

        // Create specific date
        LocalDate birthday = LocalDate.of(1990, Month.JANUARY, 15);
        System.out.println("Birthday: " + birthday);

        // Parse from string
        LocalDate parsed = LocalDate.parse("2025-10-06");
        System.out.println("Parsed: " + parsed);
    }
}

LocalDate Operations

LocalDate provides many useful methods for date manipulation:
LocalDate today = LocalDate.now();

// Add/subtract days, months, years
LocalDate nextWeek = today.plusDays(7);
LocalDate nextMonth = today.plusMonths(1);
LocalDate lastYear = today.minusYears(1);

// Get specific values
int year = today.getYear();
int month = today.getMonthValue();
int day = today.getDayOfMonth();
String dayOfWeek = today.getDayOfWeek().toString();

// Check relationships
boolean isBefore = today.isBefore(nextWeek);  // true
boolean isAfter = today.isAfter(lastYear);     // true

Working with LocalDateTime

LocalDateTime combines date and time (without timezone):
import java.time.LocalDateTime;
import java.time.Month;

public class LocalDateTimeExample {
    public static void main(String[] args) {
        // Get current date and time
        LocalDateTime now = LocalDateTime.now();
        System.out.println("Now: " + now);

        // Create specific date-time
        LocalDateTime meeting = LocalDateTime.of(2025, Month.OCTOBER, 6, 14, 30);
        System.out.println("Meeting: " + meeting);

        // Manipulate date-time
        LocalDateTime nextHour = now.plusHours(1);
        LocalDateTime yesterday = now.minusDays(1);

        System.out.println("Next hour: " + nextHour);
        System.out.println("Yesterday: " + yesterday);
    }
}

Aside - The Exception

Notice the try{]catch(Exception ex){} construct : this is the way errors are handled in Java.
  • As a quick concept description, the try{} block symbolizes a part of code critical enough to throw an exception.
  • The catch(Exception e){} block allows to react while concretely facing that exception
We will dedicate a full lecture on that subject, for now consider that it is an error case

Formatting Dates with DateTimeFormatter

Use DateTimeFormatter to format and parse dates:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

// Predefined formatters
LocalDateTime now = LocalDateTime.now();
String iso = now.format(DateTimeFormatter.ISO_DATE_TIME);
System.out.println(iso); // 2025-10-06T14:30:00

// Custom formatters
DateTimeFormatter customFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd - HH:mm:ss");
String formatted = now.format(customFormatter);
System.out.println(formatted); // 2025/10/06 - 14:30:00

// Parsing from String
LocalDate parsed = LocalDate.parse("2025-10-06");
LocalDateTime parsedCustom = LocalDateTime.parse("2025/10/06 - 14:30:00", customFormatter);
        

Calculating Periods Between Dates

Use Period to calculate date-based differences:
import java.time.LocalDate;
import java.time.Period;

LocalDate birthday = LocalDate.of(1990, 1, 15);
LocalDate today = LocalDate.now();

// Calculate age
Period age = Period.between(birthday, today);
System.out.println("Age: " + age.getYears() + " years, "
                 + age.getMonths() + " months, "
                 + age.getDays() + " days");

// Calculate days between dates
long daysBetween = birthday.until(today).getDays();
System.out.println("Days since birth: " + daysBetween);

Exercise 1: Date Input Validation

Create a Java program that:
  • Asks the user to enter a date in the format yyyy-MM-dd
  • Parses the input using LocalDate.parse()
  • Validates that the date is valid (catches DateTimeParseException)
  • Displays the day of the week for that date
Hint: Use try-catch to handle invalid input

Exercise 2: Age Calculator

Create a program that:
  • Asks the user for their birth date (format: yyyy-MM-dd)
  • Calculates their exact age in years, months, and days
  • Displays how many days until their next birthday
Hint: Use Period.between() and LocalDate.plusYears()

2nd part: The String object

We now need to format properly a String variable. You already got an overview of what could be the usage of the String objects
Creation
String firstString = "Sample String";
String secondString = "Second String";
Concatenation
System.out.println("Testing concatenation");
String concatResult = firstString + " " + secondString;
System.out.println(concatResult);

2nd part: The String object (2)

Replacement
System.out.println("Testing replacement");
concatResult = concatResult.replaceAll("St" , "" );
System.out.println(concatResult);
Immutability : While other objects are "mutable" (their internal values can change during the program execution), Strings are immutable. Format
//Full spec here
//http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html
String formattedString = String.format("%tD", new Date());
System.out.println(formattedString); 

2nd part: The String object (3)

You may also need one convenience class: StringBuilder, which allows the developer to "build" a String
StringBuilder sb = new StringBuilder(concatResult);
stringBuilder.append("blah").append(" ").append(new Date()); 
Mutability : What is convenient with the StringBuilder, is that it is mutable. It allows to pass the StringBuilder to methods, and it is modified by the operations done in the method Body

Exercise 3: Modern Logger Implementation

Write a class ModernLogger with the following method:
public void log(String message){
 //Implement using LocalDateTime and DateTimeFormatter
}
Requirements:
  • Use LocalDateTime.now() to get the current timestamp
  • Format the timestamp as yyyy/MM/dd - HH:mm:ss
  • Output format: 2025/10/06 - 14:30:00 : Your message here

Exercise 4: Booking System

Create a hotel booking system that:
  • Asks for check-in and check-out dates (yyyy-MM-dd format)
  • Calculates the number of nights
  • Validates that check-out is after check-in
  • Calculates the total cost (assume 100 euros per night)
  • Displays a booking confirmation with all details

Slide Overview