LocalDateTime Usage
Part of the Logic & Flow section of Coddy's Java journey — lesson 56 of 59.
LocalDateTime in Java combines both date and time. It's useful when you need to work with both components, like event schedules or timestamps.
Create current date and time
LocalDateTime now = LocalDateTime.now();Create a specific date and time:
LocalDateTime dateTime = LocalDateTime.of(2024, 3, 15, 14, 30);
// 2024-03-15 14:30After executing the above code, dateTime contains:
2024-03-15T14:30
Format a date and time:
LocalDateTime dateTime = LocalDateTime.of(2024, 3, 15, 14, 30);
String formatted = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));Challenge
EasyCreate a method named processDateTime that takes four arguments:
- A String (
dateTimeStr) in format "yyyy-MM-dd HH:mm" - An integer (
amount) to add or subtract - A String (
unit) either "hours", "days", or "months" - A String (
operation) either "add" or "subtract"
The method should:
- Parse the input date time string
- Perform the specified operation
- Return formatted information about the result
Return messages should be:
- If invalid format: return "Invalid date time format"
- If invalid unit: return "Invalid unit"
- If invalid operation: return "Invalid operation"
- For success: return "Original: [datetime], New: [new_datetime], Day: [day_name]"
Cheat sheet
LocalDateTime combines both date and time in Java.
Create current date and time:
LocalDateTime now = LocalDateTime.now();Create a specific date and time:
LocalDateTime dateTime = LocalDateTime.of(2024, 3, 15, 14, 30);
// Results in: 2024-03-15T14:30Format a date and time:
LocalDateTime dateTime = LocalDateTime.of(2024, 3, 15, 14, 30);
String formatted = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));Try it yourself
import java.util.Scanner;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class Main {
public static String processDateTime(String dateTimeStr, int amount, String unit, String operation) {
// Write your code here
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String dateTimeStr = scanner.nextLine();
int amount = Integer.parseInt(scanner.nextLine());
String unit = scanner.nextLine();
String operation = scanner.nextLine();
System.out.println(processDateTime(dateTimeStr, amount, unit, 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