如何以天显示日期差异?

时间:2012-10-26 18:14:37

标签: android date

我需要从数据库中的值获取实际日期并减去日期,现在日期为now,数据库中的日期为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;
}

在类中设置的变量:

     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;
}

我不知道怎么做。提前谢谢。

2 个答案:

答案 0 :(得分:1)

试试这个:

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

它将获得时间日期,它将按天给出结果天数。

答案 1 :(得分: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();
}

自{3级}以来,课程DateUtils可用,但您可以随时自行计算DateUtils.DAY_IN_MILLIS的值。