HUE 03 @ POS201920
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB

  1. import java.time.LocalDate;
  2. import java.time.Period;
  3. public class Geburtstag {
  4. String name;
  5. int jahr;
  6. int monat;
  7. int tag;
  8. LocalDate datum;
  9. public Geburtstag(LocalDate datum) {
  10. this.datum = datum;
  11. }
  12. public Geburtstag(String name, int jahr, int monat, int tag) {
  13. this.name = name;
  14. this.datum = LocalDate.of(jahr, monat, tag);
  15. }
  16. public LocalDate naechsteGeburtstagsfeier() {
  17. Geburtstag temp = new Geburtstag(this.datum);
  18. LocalDate today = LocalDate.now();
  19. if (temp.datum.isAfter(today) || temp.datum.isEqual(today)) {
  20. // do nothing, birthday wasn't already or is today
  21. } else if (temp.datum.isBefore(today)) {
  22. for (int i = 0; temp.datum.isBefore(today) && !temp.datum.isEqual(today); i++) {
  23. temp.datum = temp.datum.plusYears(1);
  24. }
  25. }
  26. return temp.datum;
  27. }
  28. public int alter() {
  29. LocalDate today = LocalDate.now();
  30. int years = 0;
  31. try {
  32. years = Period.between(this.datum, today).getYears();
  33. if (this.datum.isEqual(today)) {
  34. years++;
  35. }
  36. } catch (Exception NullPointerException) {
  37. // date was invalid, do nothing
  38. }
  39. return years;
  40. }
  41. }