Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot deduct a day from current date in Java using jodatime [closed]

I use JodaTime to deduct a day from current date . But it dosen't work for me, which means, my Date remains same, nothing is deducted! .

Here is the code ,

public class Note {
    private java.sql.Date date;

    public java.sql.Date getDate() {
        return date;
    }

    public void setDate(java.sql.Date date) {
        this.date = date;
    }
}


Note notez=new Note();

notez.setDate(new java.sql.Date(new DateTime(new  Date()).minus(1).toDate().getTime()));

Have any ideas about this .

Thank you.

like image 916
Terance Wijesuriya Avatar asked Sep 09 '26 09:09

Terance Wijesuriya


2 Answers

DateTime.minus(long) will deduct the amount of milliseconds passed to the method.

What you're looking for is DateTime.minusDays(int)

Check the Javadoc

like image 106
pca Avatar answered Sep 11 '26 22:09

pca


Try this:

notez.setDate(new java.sql.Date(new DateTime(new  Date()).minusDays(1).toDate().getTime()));
like image 31
Maksym Kreshchyshyn Avatar answered Sep 12 '26 00:09

Maksym Kreshchyshyn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!