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(); // 15Parse 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-10After executing the above code:
future: 2024-03-25past: 2024-03-10
Challenge
EasyCreate a method named processDate that takes three arguments:
- A String (
dateStr) in format "yyyy-MM-dd" - An integer (
days) to add or subtract - A String (
operation) either "add" or "subtract"
The method should:
- Parse the input date string
- Add or subtract the specified number of days
- 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-10Try 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));
}
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysMatrix Addition & SubstractionJagged Arrays3D Arrays And BeyondCommon 2D Array PatternsRecap - All About Arrays4HashSet Part 1
What is a HashSet?Adding an ElementRemoving an ElementChecking if an Element ExistsEmpty and SizeClear and CloneRecap - HashSet10Date and Time
LocalDate BasicsLocalTime OperationsLocalDateTime UsagePeriod and DurationDate FormattingTime Zone Handling2HashMap Part 1
What is a HashMap?Declare a HashMapAccessing ValuesCheck If Key ExistsModifying DictionariesRecap - HashMap