2

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.

PeakGen
  • 21,894
  • 86
  • 261
  • 463
Terance Wijesuriya
  • 1,928
  • 9
  • 31
  • 61
  • new DateTime(new Date()) could be changed to DateTime.now() if that helps clear things up. (also, specifying a timezone is likely warranted). – CasualT Apr 01 '16 at 17:37

4 Answers4

4

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

pca
  • 442
  • 5
  • 18
3

Try this:

notez.setDate(new java.sql.Date(new DateTime(new  Date()).minusDays(1).toDate().getTime()));
  • 1
    just for clearness: you mean `notez.setDate(new java.sql.Date(new DateTime(new java.util.Date()).minusDays(1).toDate().getTime()));` ? – JimHawkins Apr 01 '16 at 08:12
  • Yes I mean java.util.Date, but, actually, I just copied the last line of code and corrected it. – Maxim Kreschishin Apr 01 '16 at 08:29
  • Thank both of you. – Terance Wijesuriya Apr 01 '16 at 08:37
  • 1
    I strongly suggest passing a time zone [`DateTimeZone`](http://www.joda.org/joda-time/apidocs/org/joda/time/DateTimeZone.html) to that `DateTime` constructor. Otherwise your results may vary at runtime depending on the JVM’s current default time zone (which can change at any moment). – Basil Bourque Apr 01 '16 at 17:11
1

Why not use java.util.Calendar & java.util.Date instead.

it works easy like this

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

...

    Date today = new Date();
    Calendar c = Calendar.getInstance(); 
    c.setTime(today); 
    c.add(Calendar.DATE, -1); //-1 parameter indicates number of days you wish to add/deduct,
    Date yesterday = c.getTime();



notez.setDate(yesterday);
  • 2
    Because the j.u.Date / j.u.Calendar apis are bad and Joda Time is superior in every way? – Olivier Grégoire Apr 01 '16 at 09:10
  • Don't think so i Had the Same issue using joda time i could not get it to work with joda but the Java.util. Api s Are the best no 3rd party Library to add and many more advantages. so this was the best Solution for me if it's so easy why don't You Provide an answer –  Apr 01 '16 at 10:01
  • [Many](http://stackoverflow.com/questions/1571265/why-is-the-java-date-api-java-util-date-calendar-such-a-mess) [people](http://stackoverflow.com/questions/1969442/whats-wrong-with-java-date-time-api) [disagree](http://stackoverflow.com/questions/2901262/why-were-most-java-util-date-methods-deprecated). Also, I won't provide an answer to the question above because there were other people doing it perfectly. – Olivier Grégoire Apr 01 '16 at 10:05
1

LocalDate

A java.sql.Date represents a date-only value, without time-of-day and without time zone. Unlike the unfortunately-named java.util.Date which represents both a date and a time-of-day.

The appropriate Joda-Time class would be LocalDate.

While a LocalDate stores no time zone, a time zone is crucial in determining ‘today’. Just after midnight in Paris is still ‘yesterday’ in Montréal.

DateTimeZone zone = DateTimeZone.forID( "America/Montreal" );
LocalDate today = LocalDate.now( zone );
LocalDate yesterday = today.minusDays( 1 );

Convert to a java.sql type. The LocalDate::toString method generates text formatted according to the ISO 8601 standard, YYYY-MM-DD. For a date-only values that happens to be the same as in SQL format expected by the java.sql.Date.valueOf method.

java.sql.Date sqlDate = java.sql.Date.valueOf( yesterday.toString() );

By the way… if, instead of Joda-Time, you were using the java.time framework built into Java 8 and later, the code would be nearly the same as shown here. The makers of Joda-Time have asked us to migrate to java.time as soon as is convenient.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154