I am able to add 102 days to any date I input, but now the problem is, it should be 102 week days (excluding weekends). How can I do that?
Here's my code for just adding 102 days:
private void txtStartKeyReleased(java.awt.event.KeyEvent evt) {
try {
Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(txtStart.getText());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
cal.add(Calendar.DATE, 102);
String expDateString = sdf.format(cal.getTime());
txtExpiry.setText(expDateString);
} catch (ParseException ex) {
Logger.getLogger(ClientInfo.class.getName()).log(Level.SEVERE, null, ex);
}
}
How about the following?
for (int i = 1; i <= 102; i++)
{
DateTime thisOne = DateTime.Parse("2012-03-13");
string thisDay = thisOne.ToString("dddd");
if (thisDay != "Saturday" && thisDay != "Sunday")
{
cal.add(Calendar.DATE, i);
}
}
Edit: Updated for custom date
I am just pasting the code piece which excludes the weekend alone. Please use this according to your purpose
int numberOfDays = 102;
int count = 1;
String expDateString = null;
Date temp = date1;
while(count != numberOfDays){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(temp);
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK );
if(!(dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY)){
cal.add(Calendar.DATE, count);
count++;
}
temp = cal.getTime();
expDateString = sdf.format(cal.getTime());
}
This should add weekdays
week days (weekends are ignored) onto a date. If you pass 0
for weekdays
it will get the next working day. So, Saturday + 1 weekday = Tuesday
.
private static Date addWeekdaysToDate(Date date, int weekdays) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int originalDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int numWeeks = weekdays / 5;
int remainderDays = weekdays % 5;
cal.add(Calendar.DAY_OF_MONTH, numWeeks * 7 + remainderDays);
int adjustmentDays = 0;
if (originalDayOfWeek == Calendar.SUNDAY) {
adjustmentDays = 1;
} else if (originalDayOfWeek + remainderDays > Calendar.FRIDAY) {
adjustmentDays = 2;
}
cal.add(Calendar.DAY_OF_MONTH, adjustmentDays);
return cal.getTime();
}
EDIT:
In your code, just replace with the following:
private static Date addWeekdaysToDate(Date date, int weekdays) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int originalDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
int numWeeks = weekdays / 5;
int remainderDays = weekdays % 5;
cal.add(Calendar.DAY_OF_MONTH, numWeeks * 7 + remainderDays);
int adjustmentDays = 0;
if (originalDayOfWeek == Calendar.SUNDAY) {
adjustmentDays = 1;
} else if (originalDayOfWeek + remainderDays > Calendar.FRIDAY) {
adjustmentDays = 2;
}
cal.add(Calendar.DAY_OF_MONTH, adjustmentDays);
return cal.getTime();
}
private static SimpleDateFormat inputDateFormat = new SimpleDateFormat("yyyy-MM-dd");
private void txtStartKeyReleased(java.awt.event.KeyEvent evt) {
try {
txtExpiry.setText(
inputDateFormat.format(
addWeekdaysToDate(inputDateFormat.parse(txtStart.getText()), 102)
)
);
} catch (ParseException ex) {
Logger.getLogger(ClientInfo.class.getName()).log(Level.SEVERE, null, ex);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.