Input/Output and Streams in Java
Alex Liu
Software Development Engineer
LocalDate
classLocalTime
classimport java.time.LocalDate;
import java.time.LocalTime;
.now()
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
System.out.println(date);
System.out.println(time);
2025-03-10
12:45:30.123456
DateTimeFormatter
yyyy-MM-dd
, MM/dd/yyyy
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
LocalDate
instance date
LocalDate date = LocalDate.now(); // Current format we have will be: `2025-03-10`
DateTimeFormatter
with the pattern MM/dd/yyyy
using .ofPattern()
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
DateTimeFormatter
using .format()
methodSystem.out.println(date.format(formatter))
03/10/2025
Using LocalDate.parse()
to converts text to a date
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
.plusDays()
and .minusDays()
to adjust datesLocalDate 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
.minusDays()
with value 7
LocalDate pastDate = date.minusDays(7);
System.out.println(pastDate);
pastDate
:2025-03-03
LocalDate
and LocalTime
manage dates and times separately.parse()
can converts text to a date.now()
retrieves the current date/timeDateTimeFormatter
formats and parses datesyyyy-MM-dd
, MM/dd/yyyy
.plusDays()
and .minusDays()`
Input/Output and Streams in Java