1

How do you add days to a date in SmartGwt. I found this question and found that I can use CalendarUtil.addDaysToDate(dateToAddTo, daysToAddToDateAsInteger)); but addDaysToDate() is static void. What is the point of a method that can "add days to a date" if it does not return anything?

How do I use this method? I want to do something like this.

Date newDate = dateToAddTo + daysToAddToDate;

Here is a simplified version of my code.

if (listGridRecord.getAttribute("expirationDays") != null) {
    CalendarUtil.addDaysToDate((Date) endDate.getValue(), Integer.parseInt(listGridRecord.getAttribute("expirationDays")));

    listGridRecord.setAttribute("expirationDate", (Date) endDate.getValue());
} else {
    listGridRecord.setAttributeAsJavaObject("expirationDate", null);
}

Here is a link to the javadocs

Community
  • 1
  • 1
Andrew Pullins
  • 496
  • 1
  • 14
  • 24

3 Answers3

1

This method changes the object that is passed as parameter.

Date date = new Date();
long checkBeforeChange = date.getTime();
CalendarUtil.addDaysToDate(date, 1);
long checkAfterChange = date.getTime();
if(checkBeforeChange != checkAfterChange)
    Window.alert("It works ;)");

Your code should be something like that:

if (listGridRecord.getAttribute("expirationDays") != null) {
    Date tmpDate = endDate.getValue();
    CalendarUtil.addDaysToDate(tmpDate, Integer.parseInt(listGridRecord.getAttribute("expirationDays")));
    listGridRecord.setAttribute("expirationDate", tmpDate);
} else {
    listGridRecord.setAttributeAsJavaObject("expirationDate", null);
}

When doing (Date) endDate.getValue() you get a copy of Date object thus you don't see any change.

Adam
  • 5,403
  • 6
  • 31
  • 38
  • I had that thought and checked dateToAddTo before and after the code above and it did not seem to change. I will check again to see if I was wrong. – Andrew Pullins Sep 30 '15 at 16:32
  • Just keep in mind that you should not compare the object (**reference**) before and after update. You should check the object's **value**. Simply check `date.getTime()` before and after update. – Adam Sep 30 '15 at 16:42
  • Thank you for that I will check that. Maybe that's what I did wrong last time. – Andrew Pullins Sep 30 '15 at 16:45
  • I just tried that and now I have this error: `The method getTime() is undefined for the type dateToAddTo`. – Andrew Pullins Sep 30 '15 at 17:24
  • There is a .getValue() method that I can use but after I tried that the date still did not change. – Andrew Pullins Sep 30 '15 at 17:26
  • I just copied and pasted your code into my code and it worked. How ever when I change my code around and run it the date does not change. It starts out as Aug 17 1997 and then remains Aug 17 1997 after the `CalendarUtil.addDaysToDate(date, 1);` – Andrew Pullins Sep 30 '15 at 18:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91026/discussion-between-adam-and-andrew-pullins). – Adam Sep 30 '15 at 19:44
0

I figured out what I was doing wrong with the help of @Adam. I created a new Date variable called expireationDate and set it to (Date) endDate.getValue(); after this I used it to do the calculation.

if (listGridRecord.getAttribute("expirationDays") != null) {
    Date expirationDate = (Date) endDate.getValue();
    CalendarUtil.addDaysToDate(expirationDate, Integer.parseInt(listGridRecord.getAttribute("expirationDays")));

    listGridRecord.setAttribute("expirationDate", expirationDate);
} else {
    listGridRecord.setAttributeAsJavaObject("expirationDate", null);
}
Andrew Pullins
  • 496
  • 1
  • 14
  • 24
0

First of all, you can wrap all those utility methods you need in your own utility class (private constructor), where e.g. MyDateUtilsClassName.addDays(Date, int) will return new instance, leave parameter unmodified.

When it comes to Date manipulation, in Gwt you can use standard java.util.Date methods, even those deprecated ones like setMinutes, setHours etc. Even when you see com.google.gwt.user.datepicker.client.CalendarUtil method, they are used there.

If it's not server side, but client side, you should not care much about @Deprecated on those methods. Gwt compiles them to javascript anyway. You should be aware of java.util.Calendar. As far as I remember, it is not supported at all.

Invader
  • 105
  • 1
  • 12