Java time-related programming issues

by kpkmd54461 on 2012-03-06 21:35:22

Define a variable `strDate` which can be any date (in the format: yyyy-mm-dd). Now, you need to write code to calculate the date that is 35 full working days after `strDate` (excluding weekends). I would like to see high-quality and well-considered code. Thank you.

---

**Problem Supplement:**

lucane wrote: The problem isn't very clear. Is it 35 days before or after `strDate`? And by excluding weekends, does it mean these 35 days should not include any weekends at all?

lucane wrote: The problem isn't very clear. Is it 35 days before or after `strDate`? And by excluding weekends, does it mean these 35 days should not include any weekends at all?

It must be after, javax.comm.NoSuchPortException. Let's change 35 to Chinese: "35天" → "三十五天" (35 days after `strDate`).

---

**Problem Supplement:**

q445862108 wrote: Here's the approach:

1. Calculate how many days it will be after adding 35 days to this 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); // Time after adding 35 days

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

int count = 0; // Record the number of weekend days

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 I haven't considered thoroughly... haha

---

**Solution Suggestions:**

The problem isn't very clear. Is it 35 days before or after `strDate`? And by excluding weekends, does it mean these 35 days should not include any weekends at all?

---

**Solution Suggestion:**

For this problem, you can simply add or subtract variables of the date type.

---

**Solution Suggestion:**

Approach:

1. Calculate how many days it will be after adding 35 days to this 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); // Time after adding 35 days

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

int count = 0; // Record the number of weekend days

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);

```

---

**Reference Material:**

You can refer to: JAVA programming related issues [http://www.myexception.cn/j2se/32223.html](http://www.myexception.cn/j2se/32223.html)

Related topics:

- (Beginner question) Help me check the program for errors, as it doesn't produce results (related to ArrayList).

- How to convert this time format.

- Questions about Android DatePickerDialog.