1

I need to get the actual date and minus the date from the value in the database, the date now is now and the date in the database is this.vencimento.

  public boolean getDiasVencido() {
    boolean diasvencido = false;                

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    Date datahora = null;       
    try {
        datahora = (Date) formatter.parse(this.vencimento);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Date now = new Date();      
    if(datahora.compareTo(now) < 0 ) { 


        long diff = "DATE NOW - DATE FROM DATABASE (this.vencimento)";
        long days = diff / (24 * 60 * 60 * 1000);

        diasvencido = true; 

    }
    return diasvencido;
}

the variables setted in the class:

     public class classetitulo {

private String documento;
private String vencimento;
private Double valor;
private Double multa;
private Double juros;

public classetitulo() {}

public classetitulo(String documento, String vencimento, Double valor, Double multa, Double juros) {
    this.documento = documento;
    this.vencimento = vencimento;
    this.valor = valor;
    this.juros = juros;
    this.multa = multa;
}

I don't know much how to do it. thanks in advance.

4

2 回答 2

1

尝试这个:

this.diasvencido = Integer.parseInt(""+(((now.getTime() - datahora.getTime()) / (1000 * 60 * 60 * 24))));

它将及时获取日期,并将按天计算,将结果提供给 Days。

于 2012-10-26T19:46:55.757 回答
0

你可以试试这个。这是如何计算到现在已经过去了多少天:

long now = System.currentTimeMillis();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
try {
     long dataHora = formatter.parse(this.vencimento).getTime();
     long duration = Math.abs(now - dataHora);
     long diff = duration / DateUtils.DAY_IN_MILLIS;
}
catch (ParseException e) {
     e.printStackTrace();
}

从 API 级别 3 开始可以使用DateUtils类,但您始终可以DateUtils.DAY_IN_MILLIS自己计算 的值。

于 2012-10-26T19:08:39.973 回答