import java.util.Date; import java.util.Calendar; import java.io.PrintWriter; public final class ImmutableEmployee{ private final String name; private final int id; private final Calendar hireDate; private ImmutableEmployee(String name, int id, Calendar hireDate){ this.name = name; this.id = id; this.hireDate = hireDate; } public static ImmutableEmployee getEmployeeInstance(String name, int id, Calendar cal){ Calendar calCopy = Calendar.getInstance(); calCopy.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE)); return new ImmutableEmployee(name, id, calCopy); } public String getName(){ return this.name; } public int getId(){ return this.id; } public Calendar getHireDate(){ Calendar calCopy = Calendar.getInstance(); calCopy.set(this.hireDate.get(Calendar.YEAR), this.hireDate.get(Calendar.MONTH), this.hireDate.get(Calendar.DAY_OF_MONTH), this.hireDate.get(Calendar.HOUR), this.hireDate.get(Calendar.MINUTE)); return calCopy; } public String printEmployee(){ return "Name: " + this.getName() + "\nID: " + this.getId() + "\nHire Date: " + this.getHireDate().getTime() + "\n"; } public static void main(String[] args){ PrintWriter pw = new PrintWriter(System.out, true); Calendar startCal = Calendar.getInstance(); ImmutableEmployee joe = ImmutableEmployee.getEmployeeInstance("Joe", 1, startCal); pw.println("\n== Employee Start =="); pw.println(joe.printEmployee()); pw.println("\n== Change startCal =="); startCal.set(2011, 4, 31, 12, 00); pw.println(joe.printEmployee()); pw.println("\n== Changed Retreived Date =="); Calendar mainCal = joe.getHireDate(); mainCal.set(2010, 11, 29, 12, 00); pw.println(joe.printEmployee()); } }