Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- public class Leeftijd {
- public String naam;
- private String geboortedatum;
- public void setGeboorteDatum(String dag, String maand, String jaar) {
- this.geboortedatum = jaar + "-" + maand + "-" + dag;
- }
- public Integer getLeeftijd() {
- int yearDOB = Integer.parseInt(this.geboortedatum.substring(0, 4));
- int monthDOB = Integer.parseInt(this.geboortedatum.substring(5, 7));
- int dayDOB = Integer.parseInt(this.geboortedatum.substring(8, 10));
- DateFormat dateFormat = new SimpleDateFormat("yyyy");
- java.util.Date date = new java.util.Date();
- int thisYear = Integer.parseInt(dateFormat.format(date));
- dateFormat = new SimpleDateFormat("MM");
- date = new java.util.Date();
- int thisMonth = Integer.parseInt(dateFormat.format(date));
- dateFormat = new SimpleDateFormat("dd");
- date = new java.util.Date();
- int thisDay = Integer.parseInt(dateFormat.format(date));
- int age = thisYear - yearDOB;
- if(thisMonth < monthDOB){
- age = age - 1;
- }
- if(thisMonth == monthDOB && thisDay < dayDOB){
- age = age - 1;
- }
- return age;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment