3.12 Pessoa.java

package br.aiec.negocio;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Pessoa {

    private Integer id;
    private String nome;
    private Date dataNascimento;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public Date getDataNascimento() {
        return dataNascimento;
    }

    public void setDataNascimento(Date dataNascimento) {
        this.dataNascimento = dataNascimento;
    }

    public int getIdade() {
        Calendar hoje = GregorianCalendar.getInstance();
        hoje.setTime(new Date());

        Calendar nascimento = GregorianCalendar.getInstance();
        nascimento.setTime(dataNascimento);

        int quantidadeAnos = hoje.get(Calendar.YEAR)
                - nascimento.get(Calendar.YEAR);

        nascimento.add(Calendar.YEAR, quantidadeAnos);

        if (nascimento.after(hoje)) {
            quantidadeAnos--;
        }

        return quantidadeAnos;
    }

}
Copyright © 2014 AIEC.