Skip to content

Instantly share code, notes, and snippets.

@maartenl
Created March 9, 2013 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maartenl/5125928 to your computer and use it in GitHub Desktop.
Save maartenl/5125928 to your computer and use it in GitHub Desktop.
Person.java, name, title, birth place and date. Contains some default static persons.
package history;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* The Person class, containing the important stuff about a person.
* @author mr.bear
*/
public class Person
{
public static final long MILLISECONDS_IN_A_YEAR = 365l * 24l * 60l * 60l * 1000l;
public static final Person gosling = new Person("James", "Gosling", "Father of Java", "1955-05-19", "Calgary");
public static final Person babbage = new Person("Charles", "Babbage", "Mathematician", "1791-12-26", "London");
public static final Person turing = new Person("Alan", "Turing", "Mathematician", "1912-06-23", "London");
public static final Person knuth = new Person("Donald", "Knuth", "Computer scientist", "1938-01-10", "Milwaukee");
public static final Person dijkstra = new Person("Edsger", "Dijkstra", "Computer scientist", "1930-05-11", "Rotterdam");
private String firstName;
private String lastName;
private String jobTitle;
private Date birthDate;
private String cityOfBirth;
public Person(String firstName, String lastName, String jobTitle, String birthDate, String cityOfBirth)
{
this.firstName = firstName;
this.lastName = lastName;
this.jobTitle = jobTitle;
try
{
if (birthDate != null)
{
this.birthDate = new SimpleDateFormat("yyyy-MM-dd").parse(birthDate);
}
} catch (ParseException ex)
{
Logger.getLogger(Person.class.getName()).log(Level.SEVERE, null, ex);
}
this.cityOfBirth = cityOfBirth;
}
public Person()
{
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public String getLastName()
{
return lastName;
}
public void setLastName(String lastName)
{
this.lastName = lastName;
}
public Date getBirthDate()
{
return birthDate;
}
public void setBirthDate(Date birthDate)
{
this.birthDate = birthDate;
}
public String getJobTitle()
{
return jobTitle;
}
public void setJobTitle(String jobTitle)
{
this.jobTitle = jobTitle;
}
public String getCityOfBirth()
{
return cityOfBirth;
}
public void setCityOfBirth(String cityOfBirth)
{
this.cityOfBirth = cityOfBirth;
}
public Long getAge()
{
if (birthDate == null)
{
return null;
}
Calendar now = Calendar.getInstance();
Calendar birth = Calendar.getInstance();
birth.setTime(birthDate);
return ((now.getTimeInMillis() - birth.getTimeInMillis()) / (MILLISECONDS_IN_A_YEAR));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment