Next post about very obvious thing, but I've lost about 30 minutes to find out how to do it. So this post is some kind of note, for my information.
The problem was, how to get yesterday date from datefield. I use very nice component DataPicker from Microba Controls library which deliver calendar. Ok but back to topic. How to get a day before some date selected on that component, or day after, or back to today's date. Example:
// -1 day
try {
Calendar c = Calendar.getInstance();
c.setTime(dateField.getDate()); // set calendar date from field
c.add(Calendar.DATE, -1); // substract one day
dateField.setDate(c.getTime());
} catch (PropertyVetoException ex) {
ex.printStackTrace();
}
// today
dateField.setDate(new Date());
// + 1 day
try {
Calendar c = Calendar.getInstance();
c.setTime(dateField.getDate()); // set calendar date from field
c.add(Calendar.DATE, 1); // add one day
dateField.setDate(c.getTime());
} catch (PropertyVetoException ex) {
ex.printStackTrace();
}
No comments:
Post a Comment