Count months from a date [closed]

-1

An ad will register an ad, what I want, is to get the dataCadastro and from it count 1 month, 2 months, 3 months (depends on the adm).

package br.com.petpoint.models;


import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.CreationTimestamp;

@Entity
@Table(name = "anuncio")
public class Anuncio {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Transient
    private String caminhoFoto;

    @ManyToOne
    private Local localPagina;

    @ManyToOne
    private Tempo tempoLimite;

    @JoinColumn(nullable = false)
    @ManyToOne
    private PessoaJuridica pessoaJuridica;

    @Column(nullable = false)
    @CreationTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    private Date dataCadastro;




    public Tempo getTempoLimite() {
        return tempoLimite;
    }

    public void setTempoLimite(Tempo tempoLimite) {
        this.tempoLimite = tempoLimite;
    }

    public Date getDataCadastro() {
        return dataCadastro;
    }

    public void setDataCadastro(Date dataCadastro) {
        this.dataCadastro = dataCadastro;
    }

    public Long getId() {
        return id;
    }

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

    public Local getLocalPagina() {
        return localPagina;
    }

    public void setLocalPagina(Local localPagina) {
        this.localPagina = localPagina;
    }

    public String getCaminhoFoto() {
        return caminhoFoto;
    }

    public void setCaminhoFoto(String caminhoFoto) {
        this.caminhoFoto = caminhoFoto;
    }

    public PessoaJuridica getPessoaJuridica() {
        return pessoaJuridica;
    }

    public void setPessoaJuridica(PessoaJuridica pessoaJuridica) {
        this.pessoaJuridica = pessoaJuridica;
    }
}
-----------------------------------------------------------------------
    package br.com.petpoint.models;
public enum Tempo {
  UM,DOIS,TRES
}
--------------------------------------------------------------------

package br.com.petpoint.dao;
import java.util.Date;

import br.com.petpoint.models.Tempo;

public interface TempoDAO extends DAO<Tempo> {
    public Tempo buscarPorData(Date temp);
}
---------------------------------------------------------------
    package br.com.petpoint.dao;


import java.util.Date;

import br.com.petpoint.models.Tempo;

public interface TempoDAO extends DAO<Tempo> {
    public Tempo buscarPorData(Date temp);
}
package br.com.petpoint.dao.jpa;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import br.com.petpoint.dao.TempoDAO;
import br.com.petpoint.models.Descricao;
import br.com.petpoint.models.Tempo;

public class TempoJPA implements TempoDAO {
    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void persistir(Tempo obj) {
        sessionFactory.getCurrentSession().persist(obj);

    }

    @Override
    public void alterar(Tempo obj) {
        // TODO Auto-generated method stub

    }

    @Override
    public void deletar(Tempo obj) {
        // TODO Auto-generated method stub

    }

    @Override
    public Tempo buscar(Long id) {
    return null;
    }

    @Override
    public List<Tempo> buscarTodos() {
        String hql = "FROM Tempo";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        return query.list();
    }

    @Override
    public Tempo buscarPorData(Date temp) {
        String hql = "FROM Tempo u WHERE u.tempo = :tempo";

        Query query = sessionFactory.getCurrentSession().createQuery(hql);

        query.setParameter("tempo", temp);

        List<Tempo> resultado = query.list();

        if (!resultado.isEmpty()) {
            return resultado.get(0);
        } else {
            return null;
        }
    }

}
package br.com.petpoint.jobs;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import br.com.petpoint.dao.AnuncioDAO;
import br.com.petpoint.dao.TempoDAO;
import br.com.petpoint.models.Anuncio;
import br.com.petpoint.models.Tempo;



public class TempoJobs implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private AnuncioDAO anuncioDAO;
    @Autowired
    private TempoDAO tempoDAO;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent arg0) {

    List<Anuncio> anuncios = anuncioDAO.buscarTodos();

    Date data = new Date(System.currentTimeMillis());

    for (int i = 0; i < anuncios.size(); i++) {

    /*  Date data1

        if(data.after(anuncios.get(i).getDataCadastro().setMonth(anuncios.get(i).);))
        */
    }

    }

}
    
asked by anonymous 01.11.2018 / 12:51

1 answer

0

You can use the Calendar class that makes date operations much easier on the system, here is a simple example to add a number of months to a date:

public static Date somaMesesAData(int qtdeMeses, Date data){
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(data);
    calendar.add(Calendar.MONTH, qtdeMeses);
    return calendar.getTime();
}
    
01.11.2018 / 13:25