java获取上周的周五
时间: 2023-09-26 20:09:55 浏览: 99
要获取上周的周五,可以使用Java的`java.util.Calendar`和`java.time.LocalDate`类。下面是一个示例代码:
```java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
LocalDate previousFriday = now.with(DayOfWeek.FRIDAY).minusWeeks(1);
System.out.println("上周的周五是:" + previousFriday);
}
}
```
这段代码首先使用`LocalDate.now()`获取当前日期,然后使用`with(DayOfWeek.FRIDAY)`将日期调整为当前周的周五,最后使用`minusWeeks(1)`将日期调整为上周的周五。输出结果即为上周的周五的日期。
相关问题
java如何获取上周的日期
你可以使用 Java 中的 `Calendar` 类来获取上周的日期。下面是一个获取上周日期的示例代码:
```java
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.WEEK_OF_YEAR, -1);
Date lastWeekDate = calendar.getTime();
```
首先,我们获取当前日期时间的 `Calendar` 实例。然后,使用 `add` 方法将当前日期时间向前推一周,即减去一周的时间量。最后,使用 `getTime` 方法将 `Calendar` 实例转换为 `Date` 类型的日期。这样就可以获取到上周的日期了。
java 获取本周二到下周一日期 与获取明天日期与获取上周二到本周一日期
获取本周二到下周一日期的Java代码:
```java
import java.time.DayOfWeek;
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate nextTuesday = date.with(DayOfWeek.TUESDAY);
LocalDate nextMonday = date.with(DayOfWeek.MONDAY).plusWeeks(1);
System.out.println("本周二到下周一日期:");
System.out.println("周二:" + nextTuesday);
System.out.println("周三:" + nextTuesday.plusDays(1));
System.out.println("周四:" + nextTuesday.plusDays(2));
System.out.println("周五:" + nextTuesday.plusDays(3));
System.out.println("周六:" + nextTuesday.plusDays(4));
System.out.println("周日:" + nextTuesday.plusDays(5));
System.out.println("周一:" + nextMonday);
}
}
```
获取明天日期的Java代码:
```java
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate tomorrow = LocalDate.now().plusDays(1);
System.out.println("明天日期:" + tomorrow);
}
}
```
获取上周二到本周一日期的Java代码:
```java
import java.time.DayOfWeek;
import java.time.LocalDate;
public class Main {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
LocalDate lastTuesday = date.with(DayOfWeek.TUESDAY).minusWeeks(1);
LocalDate lastMonday = date.with(DayOfWeek.MONDAY).minusWeeks(1);
System.out.println("上周二到本周一日期:");
System.out.println("上周二:" + lastTuesday);
System.out.println("上周三:" + lastTuesday.plusDays(1));
System.out.println("上周四:" + lastTuesday.plusDays(2));
System.out.println("上周五:" + lastTuesday.plusDays(3));
System.out.println("上周六:" + lastTuesday.plusDays(4));
System.out.println("上周日:" + lastTuesday.plusDays(5));
System.out.println("本周一:" + lastMonday);
}
}
```
阅读全文