Menu
Coddy logo textTech

LocalDate Basics

Part of the Logic & Flow section of Coddy's Java journey — lesson 54 of 59.

LocalDate in Java represents a date without time or timezone. It's used for working with dates like birthdays, holidays, or schedules.

Create today's date:

LocalDate today = LocalDate.now();

Create a specific date:

LocalDate date = LocalDate.of(2024, 3, 15);

After executing the above code, date contains: 2024-03-15

Get different parts of a date:

LocalDate date = LocalDate.of(2024, 3, 15);
int year = date.getYear();      // 2024
int month = date.getMonthValue(); // 3
int day = date.getDayOfMonth();   // 15

Parse a date from a String:

LocalDate date = LocalDate.parse("2024-03-15");  // Format: "yyyy-MM-dd"

Important: If the string format is invalid, parse() throws a DateTimeParseException. Use try-catch to handle this:

try {
    LocalDate date = LocalDate.parse("invalid-date");
} catch (DateTimeParseException e) {
    System.out.println("Invalid date format");
}

Add or subtract days:

LocalDate date = LocalDate.of(2024, 3, 15);
LocalDate future = date.plusDays(10);    // 2024-03-25
LocalDate past = date.minusDays(5);      // 2024-03-10

After executing the above code:

  • future: 2024-03-25
  • past: 2024-03-10
challenge icon

Challenge

Easy

Create a method named processDate that takes three arguments:

  1. A String (dateStr) in format "yyyy-MM-dd"
  2. An integer (days) to add or subtract
  3. A String (operation) either "add" or "subtract"

The method should:

  1. Parse the input date string
  2. Add or subtract the specified number of days
  3. Return formatted information about the result

Return messages should be:

  • If invalid date format: return "Invalid date format"
  • If invalid operation: return "Invalid operation"
  • For success: return "Original: [date], New: [new_date], Day of week: [day_name]"

Cheat sheet

LocalDate represents a date without time or timezone in Java.

Create today's date:

LocalDate today = LocalDate.now();

Create a specific date:

LocalDate date = LocalDate.of(2024, 3, 15);

Get different parts of a date:

LocalDate date = LocalDate.of(2024, 3, 15);
int year = date.getYear();      // 2024
int month = date.getMonthValue(); // 3
int day = date.getDayOfMonth();   // 15
String dayOfWeek = date.getDayOfWeek().toString();  // "FRIDAY"

Parse a date from a String:

LocalDate date = LocalDate.parse("2024-03-15");  // Format: "yyyy-MM-dd"

Add or subtract days:

LocalDate date = LocalDate.of(2024, 3, 15);
LocalDate future = date.plusDays(10);    // 2024-03-25
LocalDate past = date.minusDays(5);      // 2024-03-10

Try it yourself

import java.util.Scanner;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

public class Main {
    public static String processDate(String dateStr, int days, String operation) {
        // Write your code here
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String dateStr = scanner.nextLine();
        int days = Integer.parseInt(scanner.nextLine());
        String operation = scanner.nextLine();
        
        System.out.println(processDate(dateStr, days, operation));
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow