How can pass YearB to getter getAge() and save it to integer in Java : package test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class Person { private static int count; private static final int START_ID = 100000; private final int id; private Date bDate; private String name; public Person(){ count++; this.id = START_ID + count; } public Person(String date) { this(); setAge(date); } public int getId() { return id; } public int getAge() { // home work Date today=new Date(); System.out.println(today); Calendar cal = Calendar.getInstance(); int result = cal.get(Calendar.YEAR); System.out.println(result); System.out.println("exp\n"); // System.out.println(YearB); return 0; } // format: "yyyy/MM/dd" public void setAge(String date) { if(date!=null){ String format="yyyy/MM/dd"; SimpleDateFormat sdf=new SimpleDateFormat(format); String bDate="1975/03/11"; System.out.println(bDate); String Year[]=bDate.split("/"); int YearB=Integer.parseInt(Year[0]); System.out.println(YearB); // home work // fill bDate } } public void display() { String txt = "id: " + id + " age: " + getAge() + " name: " + name ; System.out.println(txt); } public static int getPersonsCount(){ return count; } } Code (markup):
To answer your question, public int getAge(int year) { int i = year; ... return i; then when you call getAge(), you pass YearB as a parameter like this getAge(YearB); However, looking at your code, you should perform all your age logic in setAge(), so that when you call getAge(), it simply returns age. I'm guessing you want to take user input date and subtract it from current date, and then save the result as age? This is how I would do it: Add a field called age (private int age). Perform the calculations in setAge() and save the result into your age field. getAge() should return age instead of 0. When you call getAge() in display(), it will return the correct age.