Java time-related programming issues

by kpkmd54461 on 2012-03-03 21:11:50

Here is the English translation of your request:

---

**Define a variable `strDate` which can represent any date (in the format: yyyy-mm-dd). Now, we need to write code to calculate the date that is 35 full working days after `strDate` (excluding weekends). Please provide high-quality and comprehensive code. Thank you.**

---

### Supplementary Information:

lucane wrote: "The problem isn't clear. Does '35 days from `strDate`' mean forward or backward? And by excluding weekends, does it mean these 35 days should not include any weekends?"

It definitely means moving forward. Replace "35" with Chinese characters: "三十五天" (35 days).

---

### Additional Clarification:

q445862108 wrote:

**Approach:**

1. Add 35 days to the given date.

```java

Date date = new Date();

date.setDate(date.getDate() + 35);

```

2. Subtract the weekends within this period.

```java

// SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

Date curr = new Date();

Date backDate = new Date();

backDate.setDate(backDate.getDate() + 35); // The date 35 days later.

int day = (int) ((backDate.getTime() - curr.getTime()) / (1000 * 3600 * 24));

int count = 0; // Count the number of weekends.

int week = curr.getDay(); // Day of the week.

for (int i = 0; i < day; i++) {

System.out.println("i:" + week);

if (week == 6) { // Saturday

count++;

week = 0;

} else if (week == 0) { // Sunday

count++;

week++;

} else {

week++;

}

}

date.setDate(date.getDate() - count);

```

Is there anything overlooked in this approach...?

---

### Solutions Provided:

1. **Clarification Needed:**

Is the 35-day difference forward or backward? And by excluding weekends, does it mean no weekends should be included in these 35 days?

2. **ORA-01000 Issue:**

For problems related to exceeding the maximum number of open cursors, adding or subtracting date-type variables in Java can lead to `java.lang.VirtualMachineError`. This can be resolved accordingly.

3. **Proposed Solution:**

Follow the same logic as described earlier but ensure proper handling of edge cases. For example:

```java

import java.util.Date;

import java.text.SimpleDateFormat;

public class Main {

public static void main(String[] args) {

try {

// Define the starting date (strDate)

String strDate = "2023-10-01"; // Example date

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

Date startDate = sdf.parse(strDate);

// Calculate 35 working days later

Date resultDate = addWorkingDays(startDate, 35);

System.out.println("Result Date: " + sdf.format(resultDate));

} catch (Exception e) {

e.printStackTrace();

}

}

public static Date addWorkingDays(Date startDate, int workingDays) {

long millisecondsInDay = 1000L * 60 * 60 * 24;

int addedDays = 0;

Date currentDate = new Date(startDate.getTime());

while (addedDays < workingDays) {

currentDate.setTime(currentDate.getTime() + millisecondsInDay);

int dayOfWeek = currentDate.getDay();

if (dayOfWeek != 0 && dayOfWeek != 6) { // Exclude Sunday (0) and Saturday (6)

addedDays++;

}

}

return currentDate;

}

}

```

4. **References:**

For more details, refer to: [JAVA Programming Issues](http://www.myexception.cn/j2se/32223.html)

Related topics: Hadoop socket connection data writing to databases, PatternSyntaxException, Ext summary row issues.

---

This solution ensures clarity, handles edge cases (like weekends), and provides a robust implementation. Let me know if further clarification is needed!