gpt4 book ai didi

java - 如何在日历上设置某些日子。DAY_OF_WEEK

转载 作者:行者123 更新时间:2023-12-01 14:13:47 24 4
gpt4 key购买 nike

我需要在方法集中设置一些天。我尝试使用:

c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);

但这种方式只设置在星期三。

谢谢你,对我的英语感到抱歉:)

最佳答案

Calendar不按您的预期运行。来自 JavaDoc:

The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week. An instant in time can be represented by a millisecond value that is an offset from the Epoch, January 1, 1970 00:00:00.000 GMT (Gregorian).

请注意,文档指出 specific instant in time 。这意味着 Calendar只能基于纪元中的一个时间点。

当您使用set时您正在调整specific instant in time的方法通过每次通话。因此,首先将其设置为星期一,然后是星期三。

您可以使用List<Calendar>存储多个Calendar实例设置为您所需的天数。

public class CalendarTest {
public static void main(String[] args) {
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
cal2.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);

List<Calendar> calendars = Arrays.asList(cal1, cal2);
}
}

关于java - 如何在日历上设置某些日子。DAY_OF_WEEK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18294014/

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