gpt4 book ai didi

java - 试图获取本周星期一的日期

转载 作者:搜寻专家 更新时间:2023-11-01 02:28:28 28 4
gpt4 key购买 nike

我已经实现了一个方法来获取本周星期一的日期,并且我已将星期一指定为一周的第一天。

但是,无论我做什么,它都会返回 Sun Mar 24 15:03:07 GMT 2013。我看不出这里有什么问题。有人能帮忙吗?

public static Date getFirstDayOfWeekDate()
{
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(2);
cal.set(Calendar.DAY_OF_WEEK,
cal.getActualMinimum(Calendar.DAY_OF_WEEK));
Date firstDayOfTheWeek = cal.getTime();
return firstDayOfTheWeek;
}

最佳答案

试试这个:

public static Date getFirstDayOfWeekDate() {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_WEEK,
cal.getActualMinimum(Calendar.DAY_OF_WEEK));
Date now = new Date();
cal.setTime(now);
int week = cal.get(Calendar.DAY_OF_WEEK);
return new Date(now.getTime() - 24 * 60 * 60 * 1000 * (week - 1));
}

关于java - 试图获取本周星期一的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15503105/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com