PDA

View Full Version : DatePicker : +1day, -1day



valery.stroeder
28 Jan 2010, 6:57 AM
Hello,

I have a DateField with 2 buttons : "previous day" and "next day"
I tried this kind of code in the "next day" button's litener.

Date current = currentDayField.getValue();

Calendar cal = new GregorianCalendar();
cal.setTime(current);
cal.add(Calendar.DATE, +1);
currentDayField.setValue(cal.getTime());


But calendar is not present in emulation library.

Is there a clean solution for this ?

micgala
28 Jan 2010, 7:44 AM
Hi, since GWT does not includes the Calendar, you have to use the Date like this:



newDate.setDate(previousDate.getDate() + 1);


Regards,
Michel.

valery.stroeder
28 Jan 2010, 10:00 AM
Thanks for your help.

But with this code there is a bogus when it changes the month...
Supposing the format 'dd/mm/yyyy' here are the dates I obtain when doing "next day"

31/01/2010
01/02/2010
02/01/2010
03/01/2010

micgala
29 Jan 2010, 2:58 AM
What is the code your using for this?

I don't have this behaviour here.

valery.stroeder
29 Jan 2010, 3:08 AM
final ToolButton dayBeforeButton;
final DateField currentDayField;
final ToolButton dayAfterButton;

currentDayField = new DateField();
currentDayField.setValue(new Date());
dayBeforeButton = new ToolButton("x-tool-left");
dayBeforeButton.addSelectionListener(new SelectionListener<IconButtonEvent>() {
@Override
public void componentSelected(final IconButtonEvent iconButtonEvent) {
Date d = new Date();
d.setDate(currentDayField.getValue().getDate() - 1);
currentDayField.setValue(d);
}
});
dayAfterButton = new ToolButton("x-tool-right");
dayAfterButton.addSelectionListener(new SelectionListener<IconButtonEvent>() {
@Override
public void componentSelected(final IconButtonEvent iconButtonEvent) {
Date d = new Date();
d.setDate(currentDayField.getValue().getDate() + 1);
currentDayField.setValue(d);
}
});

bogusky
15 Feb 2010, 1:06 AM
you could use DateWrapper and addDays(int) method

http://www.extjs.com/deploy/gxtdocs/com/extjs/gxt/ui/client/util/DateWrapper.html#addDays%28int%29 (http://www.extjs.com/forum/../deploy/gxtdocs/com/extjs/gxt/ui/client/util/DateWrapper.html#addDays%28int%29)


ContentPanel panel = new ContentPanel(new FitLayout());
ToolButton dayBeforeButton;
final DateField currentDayField;
ToolButton dayAfterButton;

currentDayField = new DateField();
currentDayField.getPropertyEditor().setFormat(DateTimeFormat.getFormat("dd.MM.yyyy"));
currentDayField.setValue(new DateWrapper().asDate());
dayBeforeButton = new ToolButton("x-tool-left");
dayBeforeButton.addSelectionListener(new SelectionListener<IconButtonEvent>() {
@Override
public void componentSelected(final IconButtonEvent iconButtonEvent) {
DateWrapper newDate = new DateWrapper(currentDayField.getValue()).addDays(-1);
currentDayField.setValue(newDate.asDate());
}
});
dayAfterButton = new ToolButton("x-tool-right");
dayAfterButton.addSelectionListener(new SelectionListener<IconButtonEvent>() {
@Override
public void componentSelected(final IconButtonEvent iconButtonEvent) {
DateWrapper newDate = new DateWrapper(currentDayField.getValue()).addDays(1);
currentDayField.setValue(newDate.asDate());
}
});

ToolBar toolbar = new ToolBar();
toolbar.add(dayBeforeButton);
toolbar.add(currentDayField);
toolbar.add(dayAfterButton);

panel.setTopComponent(toolbar);

RootPanel.get().add(panel);


final ToolButton dayBeforeButton;
final DateField currentDayField;
final ToolButton dayAfterButton;

currentDayField = new DateField();
currentDayField.setValue(new Date());
dayBeforeButton = new ToolButton("x-tool-left");
dayBeforeButton.addSelectionListener(new SelectionListener<IconButtonEvent>() {
@Override
public void componentSelected(final IconButtonEvent iconButtonEvent) {
Date d = new Date();
d.setDate(currentDayField.getValue().getDate() - 1);
currentDayField.setValue(d);
}
});
dayAfterButton = new ToolButton("x-tool-right");
dayAfterButton.addSelectionListener(new SelectionListener<IconButtonEvent>() {
@Override
public void componentSelected(final IconButtonEvent iconButtonEvent) {
Date d = new Date();
d.setDate(currentDayField.getValue().getDate() + 1);
currentDayField.setValue(d);
}
});