Introduction to Date and Time in Java

Input/Output and Streams in Java

Alex Liu

Software Development Engineer

Creating Date objects

  • The LocalDate class
    • Represents dates without time
  • The LocalTime class
    • Represents time without date
  • Import required classes
import java.time.LocalDate;
import java.time.LocalTime;
Input/Output and Streams in Java

Creating Date objects(continued)

  • Retrieve current date and time using .now()
    LocalDate date = LocalDate.now();
    LocalTime time = LocalTime.now();
    System.out.println(date);
    System.out.println(time);
    
  • (Note: The time will vary based on when the program is executed)
2025-03-10
12:45:30.123456
Input/Output and Streams in Java

Formatting Dates in Java

  • DateTimeFormatter
    • Define custom date formats
    • Supports multiple patterns like yyyy-MM-dd, MM/dd/yyyy
  • Import necessary classes
    import java.time.LocalDate;
    import java.time.format.DateTimeFormatter;
    
Input/Output and Streams in Java

Formatting Dates in Java(continued)

  • Create a LocalDate instance date
LocalDate date = LocalDate.now(); // Current format we have will be: `2025-03-10`
  • Create a DateTimeFormatter with the pattern MM/dd/yyyy using .ofPattern()
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
  • Apply the DateTimeFormatter using .format() method
System.out.println(date.format(formatter))
03/10/2025
Input/Output and Streams in Java

Parsing String into Date

  • Using LocalDate.parse() to converts text to a date

    • Ensure the format matches the input string
  • Use .parse() with a text in yyyy-MM-dd format to parse into a Date object

    LocalDate parsedDate = LocalDate.parse("2024-03-10");
    System.out.println(parsedDate);
    
  • Print the Date object will output:

    2024-03-10
    
Input/Output and Streams in Java

Performing Date adjustment

  • Use .plusDays() and .minusDays() to adjust dates
LocalDate date = LocalDate.now();
// Current value:
System.out.println(date); 
// Apply `.plusDays()` with value `7`
LocalDate futureDate = date.plusDays(7);
// Value after adjustment
System.out.println(futureDate);
2025-03-10
2025-03-17
Input/Output and Streams in Java

Performing Date adjustment(continued)

  • Apply .minusDays() with value 7
    LocalDate pastDate = date.minusDays(7);
    System.out.println(pastDate);
    
  • Current value for pastDate:
    2025-03-03
    
Input/Output and Streams in Java

Summary

  • LocalDate and LocalTime manage dates and times separately
    • Note: The time will vary based on when the program is executed
    • .parse() can converts text to a date
    • .now() retrieves the current date/time
  • DateTimeFormatter formats and parses dates
    • define custom date formats
    • Supports multiple patterns like yyyy-MM-dd, MM/dd/yyyy
  • Perform date calculations using .plusDays() and .minusDays()`
Input/Output and Streams in Java

Let's practice!

Input/Output and Streams in Java

Preparing Video For Download...