Seydie

Class Leeftijd Calculator

Jun 23rd, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. import java.text.DateFormat;
  2. import java.text.SimpleDateFormat;
  3.  
  4. public class Leeftijd {
  5.     public String naam;
  6.     private String geboortedatum;
  7.    
  8.     public void setGeboorteDatum(String dag, String maand, String jaar) {
  9.         this.geboortedatum = jaar + "-" + maand + "-" + dag;
  10.     }
  11.  
  12.     public Integer getLeeftijd() {
  13.         int yearDOB = Integer.parseInt(this.geboortedatum.substring(0, 4));
  14.         int monthDOB = Integer.parseInt(this.geboortedatum.substring(5, 7));
  15.         int dayDOB = Integer.parseInt(this.geboortedatum.substring(8, 10));
  16.  
  17.         DateFormat dateFormat = new SimpleDateFormat("yyyy");
  18.         java.util.Date date = new java.util.Date();
  19.         int thisYear = Integer.parseInt(dateFormat.format(date));
  20.  
  21.         dateFormat = new SimpleDateFormat("MM");
  22.         date = new java.util.Date();
  23.         int thisMonth = Integer.parseInt(dateFormat.format(date));
  24.  
  25.         dateFormat = new SimpleDateFormat("dd");
  26.         date = new java.util.Date();
  27.         int thisDay = Integer.parseInt(dateFormat.format(date));
  28.         int age = thisYear - yearDOB;
  29.  
  30.         if(thisMonth < monthDOB){
  31.         age = age - 1;
  32.         }
  33.         if(thisMonth == monthDOB && thisDay < dayDOB){
  34.         age = age - 1;
  35.         }
  36.         return age;
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment