Input/Output dan Stream di Java
Alex Liu
Software Development Engineer
LocalDateLocalTimeimport 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
DateTimeFormatteryyyy-MM-dd, MM/dd/yyyyimport java.time.LocalDate;
import java.time.format.DateTimeFormatter;
LocalDate bernama dateLocalDate date = LocalDate.now(); // Format saat ini: `2025-03-10`
DateTimeFormatter dengan pola MM/dd/yyyy menggunakan .ofPattern()DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
DateTimeFormatter dengan metode .format()System.out.println(date.format(formatter))
03/10/2025
Gunakan LocalDate.parse() untuk mengonversi teks menjadi tanggal
Gunakan .parse() dengan teks berformat yyyy-MM-dd untuk mengurai ke objek Date
LocalDate parsedDate = LocalDate.parse("2024-03-10");
System.out.println(parsedDate);
Mencetak objek Date akan menghasilkan:
2024-03-10
.plusDays() dan .minusDays() untuk menyesuaikan tanggalLocalDate date = LocalDate.now();
// Nilai saat ini:
System.out.println(date);
// Terapkan `.plusDays()` dengan nilai `7`
LocalDate futureDate = date.plusDays(7);
// Nilai setelah penyesuaian
System.out.println(futureDate);
2025-03-10
2025-03-17
.minusDays() dengan nilai 7LocalDate pastDate = date.minusDays(7);
System.out.println(pastDate);
pastDate:2025-03-03
LocalDate dan LocalTime mengelola tanggal dan waktu secara terpisah.parse() mengonversi teks menjadi tanggal.now() mengambil tanggal/waktu saat iniDateTimeFormatter memformat dan mengurai tanggalyyyy-MM-dd, MM/dd/yyyy.plusDays() dan .minusDays()Input/Output dan Stream di Java