import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Date {
private int day;
private int month;
private int year;
public Date(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public void addDays(int days) {
LocalDate currentDate = LocalDate.of(year, month, day);
LocalDate newDate = currentDate.plusDays(days);
this.day = newDate.getDayOfMonth();
this.month = newDate.getMonthValue();
this.year = newDate.getYear();
}
@Override
public String toString() {
LocalDate date = LocalDate.of(year, month, day);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“dd-MM-yyyy”);
return date.format(formatter);
}
public static void main(String[] args) {
Date date = new Date(15, 8, 2023);
System.out.println(“Original Date: ” + date);
int daysToAdd = 30;
date.addDays(daysToAdd);
System.out.println(“New Date after adding ” + daysToAdd + ” days: ” + date);
}
}
Leave a Reply